1
var timeoutHandle;
function countdown(minutes,stat) {
    var seconds = 60;
    var mins = minutes;

    if(getCookie("minutes")&&getCookie("seconds")&&stat)
    {
         var seconds = getCookie("seconds");
         var mins = getCookie("minutes");
    }

    function tick() {

        var counter = document.getElementById("demo");
        setCookie("minutes",mins,1);
        setCookie("seconds",seconds,1);
        var current_minutes = mins-1
        seconds--;
        counter.innerHTML = 
        current_minutes.toString() + ":" + (seconds < 10 ? "0" : "") + String(seconds);
        //save the time in cookie
        if(current_minutes.toString()== 30)
        {
            $("#myModal").modal();

        }


        if( seconds > 0 ) {
            timeoutHandle=setTimeout(tick, 1000);
        } else {

            if(mins > 1){

               /* countdown(mins-1);   never reach “00″ issue solved:
                  Contributed by Victor Streithorst */    
               setTimeout(function () { countdown(parseInt(mins)-1,false); }, 1000);

            }
        }
    }
    tick();
}
function setCookie(cname,cvalue,exdays) {
    if(exdays > 0)
    {
    var d = new Date();
    d.setTime(d.getTime() + (exdays*24*60*60*1000));
    var expires = "expires=" + d.toGMTString();
    document.cookie = cname+"="+cvalue+"; "+expires;
    }else{

        var expires="expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/;";
        document.cookie = cname+"="+cvalue+"; "+expires;
    }
}
 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 "";
}
countdown(60,true);

$("li a").click(function(){
    var cookies = document.cookie.split(";");

for (var i = 0; i < cookies.length; i++)

$.cookie("minutes", null, { path: '/' });
$.cookie("seconds", null, { path: '/' });

});

I have two variables in above code. When I am trying to logout, both variables which are previously set in cookie, are not going to reset. Please let me know what I am doing wrong here. Thanks in advance.

Reporter
  • 3,897
  • 5
  • 33
  • 47
  • Isn't it the exact purpose of a cookie - to be preserved between several visits to a web resource? Why do you expect they'd be reset? Have you done something to reset them anywhere? – M. Prokhorov Nov 27 '17 at 13:27
  • yes i have added this code on logout in my applcation $("li a").click(function(){ var cookies = document.cookie.split(";"); for (var i = 0; i < cookies.length; i++) $.cookie("minutes", null, { path: '/' }); $.cookie("seconds", null, { path: '/' }); }); – Saurav Sony Nov 27 '17 at 13:29
  • I see no notion of "logging out" in that code. – M. Prokhorov Nov 27 '17 at 13:30

1 Answers1

0

You might want to remove the cookies those variables are reading from in your logout function:

try look at this: How to delete a cookie?

in particular :

function removeItem(sKey, sPath, sDomain) {
document.cookie = encodeURIComponent(sKey) + 
              "=; expires=Thu, 01 Jan 1970 00:00:00 GMT" + 
              (sDomain ? "; domain=" + sDomain : "") + 
              (sPath ? "; path=" + sPath : "");
}

removeItem("cookieName");