Javascript cookie timeout with countdown timer

I want to set a cookie with javascript. Easy enough. Suppose I set it to 15 minutes.

How do I read the countdown timer to show when a cookie expires? And even if they leave the page, I would like it to continue to count, and when they return to the page, it will still count.

Sorry for the poor explanation. But I'm sure it is possible.

thanks

+5
source share
1 answer

Keep a time stamp of + 15 minutes in a cookie if a cookie does not exist. Write a simple script that checks the difference between the current timestamp and the timestamp every second.

:

// 200 seconds countdown
var countdown = 200; 

//current timestamp
var now   = Date.parse(new Date());

//ready should be stored in your cookie
var ready = Date.parse(new Date (now + countdown  * 1000)); // * 1000 to get ms


//every 1000 ms
setInterval(function()
{
    var sec = ( ready - Date.parse(new Date()) )/1000;
    document.title = sec + " seconds left";

},1000);
+5

All Articles