Ashutosh
Ashutosh

Reputation: 25

how to store timer value in cookie

i am working on a online examination system app i have create a countdown timer(java script) which counts from 29 minutes and 59 seconds to 0 minutes o seconds..i want to store timers value into cookie but i don't have much idea about asp .net cookies please help me to solve this problem.

code for timer

 <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;

Upvotes: 0

Views: 388

Answers (1)

Vivek
Vivek

Reputation: 36

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;
}

Upvotes: 1

Related Questions