countdown does not return from where it left off, after pressing f5 it returns at the beginning !!! help me please
var x = setInterval(function() {
// Get todays date and time
var now = new Date().getTime();
// Find the distance between now an the count down date
var distance = deadline - now;
// Time calculations for days, hours, minutes and seconds
var days = Math.floor(distance / (1000 * 60 * 60 * 24));
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
// Display the result in the element with id="demo"
document.getElementById("dday").innerHTML = days + "d " + hours + "h " +
minutes + "m " + seconds + "s ";
// If the count down is finished, write some text
if (distance < 0) {
clearInterval(x);
document.getElementById("dday").innerHTML = "SITE NA TEIA";
}
}, 1000);
function getCurrentDeadline() {
var deadline = document.cookie.match(/(^|;)myClock=([^;]+)/);
return deadline && new Date(deadline[2]);
}
function initDeadline(){
var now = new Date().getTime();
var timeInMinutes = 10;
var deadline = new Date(now + timeInMinutes * 60 * 1000);
document.cookie = 'myClock=' + deadline + '; path=/; domain=.epizy.com' + window.location.hostname;
return deadline;
}
var deadline = getCurrentDeadline() || initDeadline();
It seems to be your cookie code that’s giving trouble.
It might help if you use more reliable ways to set and read cookies. The following functions have become standard when it comes to that.
function setCookie(name, value, days) {
var expires = "";
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
expires = "; expires=" + date.toUTCString();
}
document.cookie = name + "=" + (value || "") + expires + "; path=/";
}
function getCookie(name) {
var nameEQ = name + "=";
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, c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
}
return null;
}
function eraseCookie(name) {
document.cookie = name + "=; Max-Age=-99999999;";
}
var x = setInterval(function() {
// Get todays date and time
var now = new Date().getTime();
// Find the distance between now an the count down date
var distance = deadline - now;
// Time calculations for days, hours, minutes and seconds
var days = Math.floor(distance / (1000 * 60 * 60 * 24));
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
// Display the result in the element with id="demo"
document.getElementById("dday").innerHTML = days + "d " + hours + "h " +
minutes + "m " + seconds + "s ";
// If the count down is finished, write some text
if (distance < 0) {
clearInterval(x);
document.getElementById("dday").innerHTML = "SITE NA TEIA";
}
}, 1000);
function getCurrentDeadline() {
var deadline = document.cookie.match(/(^|;)myClock=([^;]+)/);
return deadline && new Date(deadline[2]);
}
function initDeadline() {
var now = new Date().getTime();
var timeInMinutes = 10;
var deadline = new Date(now + timeInMinutes * 60 * 1000);
document.cookie = 'myClock=' + deadline + '; path=/; domain=.epizy.com' + window.location.hostname;
return deadline;
}
var deadline = getCurrentDeadline() || initDeadline();
still unsuccessful, but the cookie looks ok, what’s missing? thanks
var x = setInterval(function() {
// Get todays date and time
var now = new Date().getTime();
// Find the distance between now an the count down date
var distance = deadline - now;
// Time calculations for days, hours, minutes and seconds
var days = Math.floor(distance / (1000 * 60 * 60 * 24));
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
// Display the result in the element with id="demo"
document.getElementById("dday").innerHTML = days + "d " + hours + "h " +
minutes + "m " + seconds + "s ";
// If the count down is finished, write some text
if (distance < 0) {
clearInterval(x);
document.getElementById("dday").innerHTML = "SITE NA TEIA";
}
}, 1000);
function getCurrentDeadline() {
var deadline = Cookie.get("myClock");
return deadline && new Date(deadline[2]);
}
function initDeadline() {
var now = new Date().getTime();
var timeInMinutes = 10;
var deadline = new Date(now + timeInMinutes * 60 * 1000);
Cookie.set("myClock",deadline);
return deadline;
}
var deadline = getCurrentDeadline() || initDeadline();![abc|690x355]
The first time that I run the code it works fine, the second time onwards it has trouble, showing NaN for each number.
The problem comes from confusions in how the date is being handled. Those issues are all fixed when you ensure that the myClock cookie value is a getTime() string, and you keep all of the other workings of the time as that getTime() number.
Clear out the old cookie:
Cookie.delete("myClock");
var deadline = getCurrentDeadline() || initDeadline();
Use getTime() as the deadline when setting the cookie:
// var deadline = new Date(now + timeInMinutes * 60 * 1000);
var deadline = new Date(now + timeInMinutes * 60 * 1000).getTime();
Cookie.set("myClock", deadline);
Don’t mess with the cookie value when retrieving it.
function getCurrentDeadline() {
var deadline = Cookie.get("myClock");
// return deadline && new Date(deadline[2]);
return deadline;
}
and after running the code, remove that delete cookie command.
// Cookie.delete("myClock");
var deadline = getCurrentDeadline() || initDeadline();
sorry to be incredibly boring, to do everything but without success, it seems that cookies are not being created
var x = setInterval(function() {
// Get todays date and time
var now = new Date().getTime();
// Find the distance between now an the count down date
var distance = deadline - now;
// Time calculations for days, hours, minutes and seconds
var days = Math.floor(distance / (1000 * 60 * 60 * 24));
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
// Display the result in the element with id="demo"
document.getElementById("dday").innerHTML = days + "d " + hours + "h " +
minutes + "m " + seconds + "s ";
// If the count down is finished, write some text
if (distance < 0) {
clearInterval(x);
document.getElementById("dday").innerHTML = "SITE NA TEIA";
}
}, 1000);
function getCurrentDeadline() {
var deadline = Cookie.get("myClock");
//return deadline && new Date(deadline[2]);
return deadline;
}
function initDeadline() {
var now = new Date().getTime();
var timeInMinutes = 10;
var deadline = new Date(now + timeInMinutes * 60 * 1000).getTime();
Cookie.set("myClock", deadline);
return deadline;
}
//Cookie.delete("myClock");
var deadline = initDeadline() || getCurrentDeadline();