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 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化