Javascript Timer应在Web浏览器关闭后继续运行

时间:2016-08-10 05:34:45

标签: javascript web-applications timer

我为Javascript计时器编写了以下代码并进行了一些验证。

function timedcounter(){
    time=time+1;
    t = setTimeout(function(){ timedcounter() }, 1000);
    display(time);
}

function starttimer() {
    var autotask=document.getElementById("Tasks").value

    if(autotask=="") {
        $("#errormsg2").show();
    } else {
        if(!timer_is_on){
            add_row_flag=1;
            var value=document.getElementById("startstopbutton").value
            if(value=="Start") {
                timer_is_on=1;
                value="Stop";
                document.getElementById("startstopbutton").innerHTML="Stop"
                $("#errormsg2").hide();
                timedcounter();
            }
        } else {
            add_row_flag=0;
            stoptimer();
        }
    }
}



function stoptimer(){
    clearTimeout(t);
    timer_is_on=0;
    document.getElementById("startstopbutton").innerHTML="Start"
    document.getElementById("disptime").value=null;

    finaltime(time);
    time=-1;
}

我想知道是否有任何方法可以在用户关闭浏览器后保持计时器运行,例如当他重新打开网页计时器时应显示自启动计时器以来经过的时间。

1 个答案:

答案 0 :(得分:1)

您有三种选择:

  1. 将计时器开始时间与时区一起存储在localStorage中,并在页面上重新打开,然后从中断处继续
  2. 使用网络服务器并在此处存储用户计时器开始时间
  3. 将其存储在cookie