如何在cookie中存储计时器值

时间:2016-07-24 18:32:57

标签: javascript asp.net

我正在研究一个在线考试系统应用程序我创建了一个倒数计时器(java脚本),从29分59秒到0分钟......我希望将计时器值存储到cookie但我不会#39对t。asp有很多想法,请帮我解决这个问题。

计时器代码

 <script type="text/javascript">


    var t;

    function display() {

        if ((document.getElementById("<%=txtMin.ClientID%>").value == 0) && (document.getElementById("<%=txtSec.ClientID%>").value == 0)) {
                //if a popup window is used:
                setTimeout("self.close()", 1000);
                return;
            }
            else {
                document.getElementById("<%=txtSec.ClientID%>").value -= 1
                document.getElementById("<%=txtMin.ClientID%>").value = document.getElementById("<%=txtMin.ClientID%>").value
                document.getElementById("<%=txtSec.ClientID%>").value = document.getElementById("<%=txtSec.ClientID%>").value
                t = setTimeout("display()", 1000);
                if (document.getElementById("<%=txtSec.ClientID%>").value <= 0) {
                document.getElementById("<%=txtSec.ClientID%>").value = 60;
                document.getElementById("<%=txtMin.ClientID%>").value -= 1;
            }
        }
    }
    window.onload = display;

1 个答案:

答案 0 :(得分:1)

var sec = parseInt(getCookie("timer")) || 1799;
var timerFunction = setInterval(function(){
  setTimer()
},1000);
function setTimer(){
  if(sec > 0){
    setCookie("timer",sec,sec);
    console.log("%d:%d",(parseInt(sec/60)),(sec%60));
    sec --;
  }else{
    clearInterval(timerFunction);
    console.log("%d:%d",(parseInt(sec/60)),(sec%60));
    console.log("Times Up !!!");
  }
}
function setCookie(name,value,sec){
  var d = new Date();
  d.setTime(d.getTime() + (sec*1000));
  document.cookie = name+"="+value+"; expires="+d.toUTCString()+"; path=/";
}

function getCookie(cname) {
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for(var i = 0; i <ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') {
            c = c.substring(1);
        }
        if (c.indexOf(name) == 0) {
            return c.substring(name.length,c.length);
        }
    }
    return false;
}