私はsetTimeout関数のタイムラグに大きな問題があります。私はボタンショーアクションのためのコードを作成しました。私はある時間にそれを行う必要があります。 My problem is that when someone open page about 30 min - 120 min before action without refresh, javascript will do time lag and my action starts much to late.
全員にとって非常に正確な時間にそのボタンを表示する必要があります。これが、PHPを使用してサーバー時間を使用してスクリプトを実行する理由です。日付はsetTimeoutで - 安全にタイムラグを短縮する方法は?
このスクリプトを修正して、表示しない、または表示を遅くするリスクを排除する方法を知っている人はいませんか?
私は約setTimeout
関数のラグについて読んで、私の問題の解決方法を探しています。
私はこのイベントで約20k〜30k人の人々がオンラインであることを期待しているたびに、私はサーバに尋ねることができないと言及する必要があります - 私はそれをテストしました。以下
私のコード:このため
(function getTime() {
$.ajax({
type : 'POST',
async: false,
url : 'time.php',
success : function(response){
// date of button display
var year = "2017",
month = "03",
day = "10",
hour = "14",
minute = "21",
second = "21";
var startDate = new Date("" + year + "-" + month + "-" + day + "T" + hour + ":" + minute + ":" + second +"+01:00");
// +01:00 timezone correction
var buttonTime = startDate.getTime()/1000; // time - when display button
var currentTime = response;
(function testTime() {
var diff = buttonTime - currentTime;
if (currentTime > buttonTime) {
// triggers here"
alert(' here will be trigger of show button');
}
if (currentTime <= buttonTime && diff <= 60) {
// test after 1 s.
var testDelay = 1000;
currentTime = parseInt(currentTime) + 1;
console.log(currentTime);
setTimeout(function() {
testTime();
}, testDelay);
}
if (currentTime < buttonTime && diff > 60) {
// test after 10s.
var testDelay = 10000;
currentTime = parseInt(currentTime) + 10;
console.log(currentTime);
setTimeout(function() {
testTime();
}, testDelay);
}
})();
},
complete(){},
error(){}
});
})();
とPHP:
<?php
$timestamp = time();
echo $timestamp;
?>
私はあなたが '$のDATE_TIME =日付($タイムスタンプ)を実現するとし;'私はそれが本当だ、ということは何も – RiggsFolly
テストしていないてきたんが、それは私の問題を解決しない:) – WojtekMG