Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/469.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在显示所有元素之前等待页面加载_Javascript_Jquery_Timer - Fatal编程技术网

Javascript 在显示所有元素之前等待页面加载

Javascript 在显示所有元素之前等待页面加载,javascript,jquery,timer,Javascript,Jquery,Timer,大家好,我有一个倒计时计时器,它使用JQuery跨多个页面显示,我使用的是本地存储: var countdowntimervalue = 100; // Some variable localStorage.setItem('timer_value', countdowntimervalue); // <-- Set the value var countdowntimervalue=100;//一些变量 localStorage.s

大家好,我有一个倒计时计时器,它使用JQuery跨多个页面显示,我使用的是本地存储:

var countdowntimervalue = 100;                            // Some variable
localStorage.setItem('timer_value', countdowntimervalue); // <-- Set the value
var countdowntimervalue=100;//一些变量

localStorage.setItem('timer_value',countdowntTimerValue);// 它以注释开始,但下面是代码

$(function() {
    var startTime = localStorage.getItem("startTime");
    if (startTime == null) {
        startTime = new Date().getTime();
        localStorage.setItem("startTime", startTime);
    }
    var countdown = setInterval(function() {
        var elapsed = Math.floor((new Date().getTime() - startTime) / 1000);
        var remaining = 60 - elapsed;
        $("span.countdown").html(remaining + " seconds Remaining");
        if (remaining == 0) {
            clearInterval(countdown);
            localStorage.removeItem("startTime");
        }
    }, 1000);
});
存储开始时间,然后计算此后经过的时间。如果你想弄得一团糟,那么你也可以计算页面之间的时间,这样你就可以得到60秒的“页面”时间,而不是60秒的页面转换时间


无论哪种方式,这都将解决计时器不停止的直接问题。

我建议存储开始时间,然后计算经过的时间。你在一段时间内所做的事情会有你所面临的问题。
$(function() {
    var startTime = localStorage.getItem("startTime");
    if (startTime == null) {
        startTime = new Date().getTime();
        localStorage.setItem("startTime", startTime);
    }
    var countdown = setInterval(function() {
        var elapsed = Math.floor((new Date().getTime() - startTime) / 1000);
        var remaining = 60 - elapsed;
        $("span.countdown").html(remaining + " seconds Remaining");
        if (remaining == 0) {
            clearInterval(countdown);
            localStorage.removeItem("startTime");
        }
    }, 1000);
});