2016-09-20 7 views
1

は、ユーザーがボタンをクリックして、時間があると言うことができます、両面リクエストサーバで行うには0Javascriptを/ノードとタイミング

最初の事を言うことができます、再び時間を取ることです、その言うことができます0今度は、currenttimenew Date().getTime())と呼ばれるvarに格納されています

その後、サーバーが動作しています。

問題は、現在の時間変数を関数に送信して保存しておき、追加すると90分(1 1/2時間)となります。

クライアント側では、ユーザーが待機する必要がある時間を取得します。 ですが、この場合の結果は90分です(exstra秒、15-30秒の間)。

サーバーにリクエストを送信してから、ユーザーが90分以上待つ必要がないことを確認するにはどうすればよいですか?

関数呼び出し:

// after processing things this occurs 
    userData.updateTimers(userid, whattotime, 5400,currenttime); 

5400は、90分のように、5400秒です。 currenttimeは、クエリ内で最初に検索されたnew Date().getTime()の変数です。

acual機能:

再び
function updateTimers(userid,type,sec,time) { 
    return new Promise(function (resolve, reject) { 
     var newtime = time + (sec * 1000); 

     var updateObj = {}; 
     updateObj[type] = newtime; 

     usertimers.update({userid: userid},{$set: updateObj}).then(function (result) { 
      console.log("updated timers"); 
      console.log(result); 
      return resolve(result); 
     }) 
    }); 
} 

は、どのように私はstureその90分にすることができ、かつ処理時間は含まれていませんか? 私は正確なタイマーを更新する必要があります。

私はAngularJSフロントエンドで平均スタックを使用しています。それは時間を取って、それを将来の使用のためにサーバー側に持っていくための回避策でしょうか、それとももっと良いことができますか?

私は、サーバーサイド行うと、いくつかの待ち時間を追加するものは何でもので、それはいつもより数秒になります:/

+0

私が正しく理解していれば、あなたの関数 'updateTimers'の終了を90分以内にしたいのですか? – MinusFour

+0

ユーザーが実際にボタンをクリックした時点から、設定した時間よりも長い時間待たなければならないということです。 – maria

+0

処理が終了していないなどの理由で90分以上かかる場合は、何を返答しますか? – MinusFour

答えて

0

離れて、テスト日付オフセットに対するティックwatchdogを使用してください。

「完了」の日付(new Date().getTime() + (1000 * 60 * 90))をユーザーに送信できます。

//Accurate timer using a watchdog 
 

 
var Watchdog = (function() { 
 
    function Watchdog(waitMilliseconds, callback) { 
 
     if (callback === void 0) { callback = function() { }; } 
 
     this.waitMilliseconds = waitMilliseconds; 
 
     this.callback = callback; 
 
     this.start = new Date().getTime(); 
 
     var self = this; 
 
     this.interval = setInterval(function() { self.test(); }, 1); 
 
    } 
 
    Watchdog.prototype.test = function() { 
 
     if (new Date().getTime() - this.start > this.waitMilliseconds) { 
 
      console.log(new Date().getTime() - this.start); 
 
      clearInterval(this.interval); 
 
      this.callback(); 
 
     } 
 
    }; 
 
    return Watchdog; 
 
}()); 
 

 
new Watchdog(1000, function() { 
 
    console.log("A second has passed"); 
 
}); 
 

 
new Watchdog(1000 * 60 * 90, function() { 
 
    console.log("90 minutes has passed"); 
 
});

+0

私もそれをしましたが、私はまだいくつかのexstra秒を取得:/ – maria

0

私はあなたの問題を理解している場合、あなたはこのPromise.timeoutのようなものを使用することができます

Promise.timeout = function(timeout, promise){ 
    return Promise.race([ 
    promise, 
    new Promise(function(resolve, reject){ 
    setTimeout(function() { reject('Timed out'); }, timeout); 
    }) 
]); 
} 

あなたはこのようにそれを使用したい:基本的に

Promise.timeout(yourRequestFunctionWhenUserClicksButton(), 1000 * 90); //90 seconds 

を、要求が90秒以上続く場合は、拒否された約束を返します。それ以外の場合は、サーバーから返された値で処理します。

関連する問題