So here I have created a countdown timer of 10 minutes to initiate upon the press of the 'START' button. I want to store this countdown so that when the user goes of the page and comes back to it the countdown will still be going. Note that I do NOT want the countdown to stop and then continue where it left off upon page reload, I would like it to continue to it's specified deadline. Any suggestions?
<html>
<head>
</head>
<body>
<a id="sendButton" class="button" onclick=" startClock();">START</a>
<div id="sectionClock">
<div id="clockdiv">
<div>
<span class="hours"></span>
<div class="smalltext">Hours</div>
</div>
<div>
<span class="minutes"></span>
<div class="smalltext">Minutes</div>
</div>
<div>
<span class="seconds"></span>
<div class="smalltext">Seconds</div>
</div>
</div>
</div>
<script>
function getTimeRemaining(endtime) {
var t = Date.parse(endtime) - Date.parse(new Date());
var seconds = Math.floor((t / 1000) % 60);
var minutes = Math.floor((t / 1000 / 60) % 60);
var hours = Math.floor((t / (1000 * 60 * 60)) % 24);
return {
'total': t,
'hours': hours,
'minutes': minutes,
'seconds': seconds
};
}
var flag = 0;
function startClock() {
/************ INITIALIZE CLOCK ************/
function initializeClock(id, endtime) {
// If countdown time is 0 then operate
if(flag==0)
{
var clock = document.getElementById(id);
var hoursSpan = clock.querySelector('.hours');
var minutesSpan = clock.querySelector('.minutes');
var secondsSpan = clock.querySelector('.seconds');
flag=1;
}
function updateClock() {
var t = getTimeRemaining(endtime);
hoursSpan.innerHTML = ('0' + t.hours).slice(-2);
minutesSpan.innerHTML = ('0' + t.minutes).slice(-2);
secondsSpan.innerHTML = ('0' + t.seconds).slice(-2);
if (t.total <= 0) {
clearInterval(timeinterval);
flag=0;
}
}
updateClock();
var timeinterval = setInterval(updateClock, 1000);
}
var timeInMinutes = 10;
var currentTime = Date.parse(new Date());
var deadline = new Date(currentTime + timeInMinutes*60*1000);
initializeClock('clockdiv', deadline);
document.cookie = deadline
var cookie = document.cookie;
console.log(cookie);
}
function sendTrack(){
(function() {
var trackUrl = document.getElementById("url");
}());
}
</script>
</body>
</html>