我在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
函数滞后,我正在寻找解决问题的方法。
我还需要提一下,我每次都不能问服务器,因为我期待在这个事件上大约有2万到3万人在线 - 我已经测试过,服务器将不会存在这么多的php查询。
我的代码如下:
(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 for this:
<?php
$timestamp = time();
echo $timestamp;
?>
答案 0 :(得分:0)
我已经解决了问题。结果如下:)我比较服务器时间和用户硬件时间,并添加差异按钮显示时间。 :)
<?php
$timestamp = time();
$date_time = date($timestamp);
echo $date_time;
?>
并且正确的time.php是:
videoTexture.minFilter = THREE.NearestFilter;