dongliao3450 2017-03-21 22:27
浏览 95
已采纳

使用setTimeout设置日期 - 如何以安全方式减少时间延迟?

I have a big problem with time lag in setTimeout function. I've created code for button show action and I need do that on certain hour. 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. I need to show that button in very accurate time for everyone - this is reason why I'm using server time (via php) to run script.

Did anyone know how to modify this script to eliminate risk of not showing or to late showing button?

I read about setTimeout function lag and I am searching solution for my problem.

I need also to mention that I cant ask server everytime beacause I am expecting about 20k - 30k people online on this event - I've tested that, severs will not survive so many php queries.

My code below:

 (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(){}
    });

})();

and php for this:

<?php 

$timestamp = time();
echo $timestamp;

?>
  • 写回答

1条回答 默认 最新

  • duanchao4445 2017-03-22 19:00
    关注

    I have solve problem. Result is below :) I compare server time nad user hardware time and add difference to button show time. :)

        function getTime() {
    
            $.ajax({
                type : 'POST',
                async:  false,
                url  : 'time.php',
                success : function(response){
    
                    // date of button display
    
                    var year = "2017",
                        month = "03",
                        day = "22",
                        hour = "13",
                        minute = "50",
                        second = "01";
    
                    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 = parseInt(response);
    
                    if (!Date.now) {
                      Date.now = function now() {
                        return new Date().getTime() / 1000;
                      };
                    }
    
                    var userPcTime = Math.floor(Date.now() / 1000);
    
                    if (currentTime !== userPcTime) {
    
                        var timeDiff = currentTime - userPcTime;
    
                        buttonTime = buttonTime - timeDiff;
    
                        console.log(timeDiff);
                    }
    
                    function testTime() {
    
                        if (!Date.now) {
                          Date.now = function now() {
                            return new Date().getTime() / 1000;
                          };
                        }
    
                        var userPcTime = Math.floor(Date.now() / 1000);
    
                         var diff = buttonTime - userPcTime;
    
    
    
                        if (userPcTime > buttonTime ) {
                            // triggers here"
    
                            alert(' here will be trigger of show button');
    
                        }
    
                        if (userPcTime <= buttonTime && diff <= 60 ) {
    
                            // test after 1 s.
    
                             var testDelay = 1000;
    
                            setTimeout(function() {
                                testTime();
                            }, testDelay);
                        }
    
                        if (userPcTime < buttonTime && diff > 60 ) {
                            // test after 10 s.
    
                            var testDelay = 10000;
    
                            setTimeout(function() {
                                testTime(); 
                            }, testDelay);
    
                        } 
    
                    };
    
                    testTime();
    
                },
                complete(){},
                error(){}
            });
    
        };
    
    getTime();
    

    And correct time.php is:

    <?php 
    
    $timestamp = time();
    $date_time = date($timestamp);
    echo $date_time;
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么