Error when I refresh count down it's restart












1















I have this code for CountDown and When I refresh the page It's count from the beginning.
I want to continue counting from where it arrived even after several times of the update:
I have this code for CountDown and When I refresh the page It's count from the beginning.
I want to continue counting from where it arrived even after several times of the update:






// Set the date we're counting down to
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);
var days = Math.floor(t / (1000 * 60 * 60 * 24));
return {
'total': t,
'days': days,
'hours': hours,
'minutes': minutes,
'seconds': seconds
};
}

function initializeClock(id, endtime) {
var clock = document.getElementById(id);
var daysSpan = clock.querySelector('.days');
var hoursSpan = clock.querySelector('.hours');
var minutesSpan = clock.querySelector('.minutes');
var secondsSpan = clock.querySelector('.seconds');

function updateClock() {
var t = getTimeRemaining(endtime);
daysSpan.innerHTML = t.days;
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);
}
}
updateClock();
var timeinterval = setInterval(updateClock, 1000);
}
var deadline = new Date(Date.parse(new Date()) + 15 * 24 * 60 * 60 * 1000);
initializeClock('clockdiv', deadline);

body {
text-align: center;
background: #ffffff;
/*font-family: sans-serif;*/
font-weight: 100;
}

h1 {
color: #e5534c;
font-weight: 900;
font-size: 40px;
margin: 40px 0px 20px;
font-family: 'Cairo', sans-serif;
}

#clockdiv {
font-family: sans-serif;
color: #fff;
display: inline-block;
font-weight: 100;
text-align: center;
font-size: 30px;
}

#clockdiv>div {
padding: 10px;
border-radius: 3px;
background: #e5534c;
display: inline-block;
}

#clockdiv div>span {
padding: 15px;
border-radius: 3px;
background: #515251;
display: inline-block;
font-family: 'Cairo', sans-serif;
}

.smalltext {
padding-top: 5px;
font-size: 16px;
color: #fff;
font-weight: 600;
font-family: 'Cairo', sans-serif;
}

<!DOCTYPE HTML>
<html>

<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://fonts.googleapis.com/css?family=Cairo" rel="stylesheet">
</head>

<body>
<h1>The remaining duration of the white Friday cuts</h1>
<div id="clockdiv">
<div>
<span class="days"></span>
<div class="smalltext">Day</div>
</div>
<div>
<span class="hours"></span>
<div class="smalltext">Hour</div>
</div>
<div>
<span class="minutes"></span>
<div class="smalltext">Minute</div>
</div>
<div>
<span class="seconds"></span>
<div class="smalltext">Second</div>
</div>
</div>
</body>

</html>












share|improve this question

























  • Save the countdown in sessionStorage, and start from there when the page is reloaded.

    – Barmar
    Nov 20 '18 at 18:15











  • Why don't you set deadline to the actual time instead of always adding 15 days to the current time?

    – Barmar
    Nov 20 '18 at 18:17











  • Thanks Barmar if you give more details how save it in sessionStorage

    – mhmt
    Nov 20 '18 at 18:32











  • and if you can rewrite the code with deadline

    – mhmt
    Nov 20 '18 at 18:33











  • this code from CodePen codepen.io/SitePoint/pen/MwNPVq

    – mhmt
    Nov 20 '18 at 18:34
















1















I have this code for CountDown and When I refresh the page It's count from the beginning.
I want to continue counting from where it arrived even after several times of the update:
I have this code for CountDown and When I refresh the page It's count from the beginning.
I want to continue counting from where it arrived even after several times of the update:






// Set the date we're counting down to
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);
var days = Math.floor(t / (1000 * 60 * 60 * 24));
return {
'total': t,
'days': days,
'hours': hours,
'minutes': minutes,
'seconds': seconds
};
}

function initializeClock(id, endtime) {
var clock = document.getElementById(id);
var daysSpan = clock.querySelector('.days');
var hoursSpan = clock.querySelector('.hours');
var minutesSpan = clock.querySelector('.minutes');
var secondsSpan = clock.querySelector('.seconds');

function updateClock() {
var t = getTimeRemaining(endtime);
daysSpan.innerHTML = t.days;
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);
}
}
updateClock();
var timeinterval = setInterval(updateClock, 1000);
}
var deadline = new Date(Date.parse(new Date()) + 15 * 24 * 60 * 60 * 1000);
initializeClock('clockdiv', deadline);

body {
text-align: center;
background: #ffffff;
/*font-family: sans-serif;*/
font-weight: 100;
}

h1 {
color: #e5534c;
font-weight: 900;
font-size: 40px;
margin: 40px 0px 20px;
font-family: 'Cairo', sans-serif;
}

#clockdiv {
font-family: sans-serif;
color: #fff;
display: inline-block;
font-weight: 100;
text-align: center;
font-size: 30px;
}

#clockdiv>div {
padding: 10px;
border-radius: 3px;
background: #e5534c;
display: inline-block;
}

#clockdiv div>span {
padding: 15px;
border-radius: 3px;
background: #515251;
display: inline-block;
font-family: 'Cairo', sans-serif;
}

.smalltext {
padding-top: 5px;
font-size: 16px;
color: #fff;
font-weight: 600;
font-family: 'Cairo', sans-serif;
}

<!DOCTYPE HTML>
<html>

<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://fonts.googleapis.com/css?family=Cairo" rel="stylesheet">
</head>

<body>
<h1>The remaining duration of the white Friday cuts</h1>
<div id="clockdiv">
<div>
<span class="days"></span>
<div class="smalltext">Day</div>
</div>
<div>
<span class="hours"></span>
<div class="smalltext">Hour</div>
</div>
<div>
<span class="minutes"></span>
<div class="smalltext">Minute</div>
</div>
<div>
<span class="seconds"></span>
<div class="smalltext">Second</div>
</div>
</div>
</body>

</html>












share|improve this question

























  • Save the countdown in sessionStorage, and start from there when the page is reloaded.

    – Barmar
    Nov 20 '18 at 18:15











  • Why don't you set deadline to the actual time instead of always adding 15 days to the current time?

    – Barmar
    Nov 20 '18 at 18:17











  • Thanks Barmar if you give more details how save it in sessionStorage

    – mhmt
    Nov 20 '18 at 18:32











  • and if you can rewrite the code with deadline

    – mhmt
    Nov 20 '18 at 18:33











  • this code from CodePen codepen.io/SitePoint/pen/MwNPVq

    – mhmt
    Nov 20 '18 at 18:34














1












1








1








I have this code for CountDown and When I refresh the page It's count from the beginning.
I want to continue counting from where it arrived even after several times of the update:
I have this code for CountDown and When I refresh the page It's count from the beginning.
I want to continue counting from where it arrived even after several times of the update:






// Set the date we're counting down to
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);
var days = Math.floor(t / (1000 * 60 * 60 * 24));
return {
'total': t,
'days': days,
'hours': hours,
'minutes': minutes,
'seconds': seconds
};
}

function initializeClock(id, endtime) {
var clock = document.getElementById(id);
var daysSpan = clock.querySelector('.days');
var hoursSpan = clock.querySelector('.hours');
var minutesSpan = clock.querySelector('.minutes');
var secondsSpan = clock.querySelector('.seconds');

function updateClock() {
var t = getTimeRemaining(endtime);
daysSpan.innerHTML = t.days;
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);
}
}
updateClock();
var timeinterval = setInterval(updateClock, 1000);
}
var deadline = new Date(Date.parse(new Date()) + 15 * 24 * 60 * 60 * 1000);
initializeClock('clockdiv', deadline);

body {
text-align: center;
background: #ffffff;
/*font-family: sans-serif;*/
font-weight: 100;
}

h1 {
color: #e5534c;
font-weight: 900;
font-size: 40px;
margin: 40px 0px 20px;
font-family: 'Cairo', sans-serif;
}

#clockdiv {
font-family: sans-serif;
color: #fff;
display: inline-block;
font-weight: 100;
text-align: center;
font-size: 30px;
}

#clockdiv>div {
padding: 10px;
border-radius: 3px;
background: #e5534c;
display: inline-block;
}

#clockdiv div>span {
padding: 15px;
border-radius: 3px;
background: #515251;
display: inline-block;
font-family: 'Cairo', sans-serif;
}

.smalltext {
padding-top: 5px;
font-size: 16px;
color: #fff;
font-weight: 600;
font-family: 'Cairo', sans-serif;
}

<!DOCTYPE HTML>
<html>

<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://fonts.googleapis.com/css?family=Cairo" rel="stylesheet">
</head>

<body>
<h1>The remaining duration of the white Friday cuts</h1>
<div id="clockdiv">
<div>
<span class="days"></span>
<div class="smalltext">Day</div>
</div>
<div>
<span class="hours"></span>
<div class="smalltext">Hour</div>
</div>
<div>
<span class="minutes"></span>
<div class="smalltext">Minute</div>
</div>
<div>
<span class="seconds"></span>
<div class="smalltext">Second</div>
</div>
</div>
</body>

</html>












share|improve this question
















I have this code for CountDown and When I refresh the page It's count from the beginning.
I want to continue counting from where it arrived even after several times of the update:
I have this code for CountDown and When I refresh the page It's count from the beginning.
I want to continue counting from where it arrived even after several times of the update:






// Set the date we're counting down to
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);
var days = Math.floor(t / (1000 * 60 * 60 * 24));
return {
'total': t,
'days': days,
'hours': hours,
'minutes': minutes,
'seconds': seconds
};
}

function initializeClock(id, endtime) {
var clock = document.getElementById(id);
var daysSpan = clock.querySelector('.days');
var hoursSpan = clock.querySelector('.hours');
var minutesSpan = clock.querySelector('.minutes');
var secondsSpan = clock.querySelector('.seconds');

function updateClock() {
var t = getTimeRemaining(endtime);
daysSpan.innerHTML = t.days;
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);
}
}
updateClock();
var timeinterval = setInterval(updateClock, 1000);
}
var deadline = new Date(Date.parse(new Date()) + 15 * 24 * 60 * 60 * 1000);
initializeClock('clockdiv', deadline);

body {
text-align: center;
background: #ffffff;
/*font-family: sans-serif;*/
font-weight: 100;
}

h1 {
color: #e5534c;
font-weight: 900;
font-size: 40px;
margin: 40px 0px 20px;
font-family: 'Cairo', sans-serif;
}

#clockdiv {
font-family: sans-serif;
color: #fff;
display: inline-block;
font-weight: 100;
text-align: center;
font-size: 30px;
}

#clockdiv>div {
padding: 10px;
border-radius: 3px;
background: #e5534c;
display: inline-block;
}

#clockdiv div>span {
padding: 15px;
border-radius: 3px;
background: #515251;
display: inline-block;
font-family: 'Cairo', sans-serif;
}

.smalltext {
padding-top: 5px;
font-size: 16px;
color: #fff;
font-weight: 600;
font-family: 'Cairo', sans-serif;
}

<!DOCTYPE HTML>
<html>

<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://fonts.googleapis.com/css?family=Cairo" rel="stylesheet">
</head>

<body>
<h1>The remaining duration of the white Friday cuts</h1>
<div id="clockdiv">
<div>
<span class="days"></span>
<div class="smalltext">Day</div>
</div>
<div>
<span class="hours"></span>
<div class="smalltext">Hour</div>
</div>
<div>
<span class="minutes"></span>
<div class="smalltext">Minute</div>
</div>
<div>
<span class="seconds"></span>
<div class="smalltext">Second</div>
</div>
</div>
</body>

</html>








// Set the date we're counting down to
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);
var days = Math.floor(t / (1000 * 60 * 60 * 24));
return {
'total': t,
'days': days,
'hours': hours,
'minutes': minutes,
'seconds': seconds
};
}

function initializeClock(id, endtime) {
var clock = document.getElementById(id);
var daysSpan = clock.querySelector('.days');
var hoursSpan = clock.querySelector('.hours');
var minutesSpan = clock.querySelector('.minutes');
var secondsSpan = clock.querySelector('.seconds');

function updateClock() {
var t = getTimeRemaining(endtime);
daysSpan.innerHTML = t.days;
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);
}
}
updateClock();
var timeinterval = setInterval(updateClock, 1000);
}
var deadline = new Date(Date.parse(new Date()) + 15 * 24 * 60 * 60 * 1000);
initializeClock('clockdiv', deadline);

body {
text-align: center;
background: #ffffff;
/*font-family: sans-serif;*/
font-weight: 100;
}

h1 {
color: #e5534c;
font-weight: 900;
font-size: 40px;
margin: 40px 0px 20px;
font-family: 'Cairo', sans-serif;
}

#clockdiv {
font-family: sans-serif;
color: #fff;
display: inline-block;
font-weight: 100;
text-align: center;
font-size: 30px;
}

#clockdiv>div {
padding: 10px;
border-radius: 3px;
background: #e5534c;
display: inline-block;
}

#clockdiv div>span {
padding: 15px;
border-radius: 3px;
background: #515251;
display: inline-block;
font-family: 'Cairo', sans-serif;
}

.smalltext {
padding-top: 5px;
font-size: 16px;
color: #fff;
font-weight: 600;
font-family: 'Cairo', sans-serif;
}

<!DOCTYPE HTML>
<html>

<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://fonts.googleapis.com/css?family=Cairo" rel="stylesheet">
</head>

<body>
<h1>The remaining duration of the white Friday cuts</h1>
<div id="clockdiv">
<div>
<span class="days"></span>
<div class="smalltext">Day</div>
</div>
<div>
<span class="hours"></span>
<div class="smalltext">Hour</div>
</div>
<div>
<span class="minutes"></span>
<div class="smalltext">Minute</div>
</div>
<div>
<span class="seconds"></span>
<div class="smalltext">Second</div>
</div>
</div>
</body>

</html>





// Set the date we're counting down to
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);
var days = Math.floor(t / (1000 * 60 * 60 * 24));
return {
'total': t,
'days': days,
'hours': hours,
'minutes': minutes,
'seconds': seconds
};
}

function initializeClock(id, endtime) {
var clock = document.getElementById(id);
var daysSpan = clock.querySelector('.days');
var hoursSpan = clock.querySelector('.hours');
var minutesSpan = clock.querySelector('.minutes');
var secondsSpan = clock.querySelector('.seconds');

function updateClock() {
var t = getTimeRemaining(endtime);
daysSpan.innerHTML = t.days;
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);
}
}
updateClock();
var timeinterval = setInterval(updateClock, 1000);
}
var deadline = new Date(Date.parse(new Date()) + 15 * 24 * 60 * 60 * 1000);
initializeClock('clockdiv', deadline);

body {
text-align: center;
background: #ffffff;
/*font-family: sans-serif;*/
font-weight: 100;
}

h1 {
color: #e5534c;
font-weight: 900;
font-size: 40px;
margin: 40px 0px 20px;
font-family: 'Cairo', sans-serif;
}

#clockdiv {
font-family: sans-serif;
color: #fff;
display: inline-block;
font-weight: 100;
text-align: center;
font-size: 30px;
}

#clockdiv>div {
padding: 10px;
border-radius: 3px;
background: #e5534c;
display: inline-block;
}

#clockdiv div>span {
padding: 15px;
border-radius: 3px;
background: #515251;
display: inline-block;
font-family: 'Cairo', sans-serif;
}

.smalltext {
padding-top: 5px;
font-size: 16px;
color: #fff;
font-weight: 600;
font-family: 'Cairo', sans-serif;
}

<!DOCTYPE HTML>
<html>

<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://fonts.googleapis.com/css?family=Cairo" rel="stylesheet">
</head>

<body>
<h1>The remaining duration of the white Friday cuts</h1>
<div id="clockdiv">
<div>
<span class="days"></span>
<div class="smalltext">Day</div>
</div>
<div>
<span class="hours"></span>
<div class="smalltext">Hour</div>
</div>
<div>
<span class="minutes"></span>
<div class="smalltext">Minute</div>
</div>
<div>
<span class="seconds"></span>
<div class="smalltext">Second</div>
</div>
</div>
</body>

</html>






javascript html css countdown






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 18:15









Barmar

424k35248349




424k35248349










asked Nov 20 '18 at 18:11









mhmtmhmt

63




63













  • Save the countdown in sessionStorage, and start from there when the page is reloaded.

    – Barmar
    Nov 20 '18 at 18:15











  • Why don't you set deadline to the actual time instead of always adding 15 days to the current time?

    – Barmar
    Nov 20 '18 at 18:17











  • Thanks Barmar if you give more details how save it in sessionStorage

    – mhmt
    Nov 20 '18 at 18:32











  • and if you can rewrite the code with deadline

    – mhmt
    Nov 20 '18 at 18:33











  • this code from CodePen codepen.io/SitePoint/pen/MwNPVq

    – mhmt
    Nov 20 '18 at 18:34



















  • Save the countdown in sessionStorage, and start from there when the page is reloaded.

    – Barmar
    Nov 20 '18 at 18:15











  • Why don't you set deadline to the actual time instead of always adding 15 days to the current time?

    – Barmar
    Nov 20 '18 at 18:17











  • Thanks Barmar if you give more details how save it in sessionStorage

    – mhmt
    Nov 20 '18 at 18:32











  • and if you can rewrite the code with deadline

    – mhmt
    Nov 20 '18 at 18:33











  • this code from CodePen codepen.io/SitePoint/pen/MwNPVq

    – mhmt
    Nov 20 '18 at 18:34

















Save the countdown in sessionStorage, and start from there when the page is reloaded.

– Barmar
Nov 20 '18 at 18:15





Save the countdown in sessionStorage, and start from there when the page is reloaded.

– Barmar
Nov 20 '18 at 18:15













Why don't you set deadline to the actual time instead of always adding 15 days to the current time?

– Barmar
Nov 20 '18 at 18:17





Why don't you set deadline to the actual time instead of always adding 15 days to the current time?

– Barmar
Nov 20 '18 at 18:17













Thanks Barmar if you give more details how save it in sessionStorage

– mhmt
Nov 20 '18 at 18:32





Thanks Barmar if you give more details how save it in sessionStorage

– mhmt
Nov 20 '18 at 18:32













and if you can rewrite the code with deadline

– mhmt
Nov 20 '18 at 18:33





and if you can rewrite the code with deadline

– mhmt
Nov 20 '18 at 18:33













this code from CodePen codepen.io/SitePoint/pen/MwNPVq

– mhmt
Nov 20 '18 at 18:34





this code from CodePen codepen.io/SitePoint/pen/MwNPVq

– mhmt
Nov 20 '18 at 18:34












1 Answer
1






active

oldest

votes


















1














Save deadline in sessionStorage.



Replace the two lines:



var deadline = new Date(Date.parse(new Date()) + 15 * 24 * 60 * 60 * 1000);
initializeClock('clockdiv', deadline);


with this:



var deadline;
var savedDeadline = sessionStorage.getItem("deadline");
if (savedDeadline) {
deadline = parseInt(savedDeadline);
} else {
deadline = new Date(Date.parse(new Date()) + 15 * 24 * 60 * 60 * 1000);
sessionStorage.setItem("deadline", deadline);
}
initializeClock('clockdiv', deadline);





share|improve this answer


























  • Thank you very much but I do not know anything about sessionStorage if you can rewrite the code fully and I thank you again

    – mhmt
    Nov 21 '18 at 6:47






  • 1





    I'm not going to rewrite the whole thing when I'm just replacing two lines. I've updated the answer to explain exactly how this fits in.

    – Barmar
    Nov 21 '18 at 16:36






  • 1





    I included a link to the sessionStorage documentation. If that's not enough, I'm sure you can find tutorials using Google.

    – Barmar
    Nov 21 '18 at 16:37











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53399054%2ferror-when-i-refresh-count-down-its-restart%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Save deadline in sessionStorage.



Replace the two lines:



var deadline = new Date(Date.parse(new Date()) + 15 * 24 * 60 * 60 * 1000);
initializeClock('clockdiv', deadline);


with this:



var deadline;
var savedDeadline = sessionStorage.getItem("deadline");
if (savedDeadline) {
deadline = parseInt(savedDeadline);
} else {
deadline = new Date(Date.parse(new Date()) + 15 * 24 * 60 * 60 * 1000);
sessionStorage.setItem("deadline", deadline);
}
initializeClock('clockdiv', deadline);





share|improve this answer


























  • Thank you very much but I do not know anything about sessionStorage if you can rewrite the code fully and I thank you again

    – mhmt
    Nov 21 '18 at 6:47






  • 1





    I'm not going to rewrite the whole thing when I'm just replacing two lines. I've updated the answer to explain exactly how this fits in.

    – Barmar
    Nov 21 '18 at 16:36






  • 1





    I included a link to the sessionStorage documentation. If that's not enough, I'm sure you can find tutorials using Google.

    – Barmar
    Nov 21 '18 at 16:37
















1














Save deadline in sessionStorage.



Replace the two lines:



var deadline = new Date(Date.parse(new Date()) + 15 * 24 * 60 * 60 * 1000);
initializeClock('clockdiv', deadline);


with this:



var deadline;
var savedDeadline = sessionStorage.getItem("deadline");
if (savedDeadline) {
deadline = parseInt(savedDeadline);
} else {
deadline = new Date(Date.parse(new Date()) + 15 * 24 * 60 * 60 * 1000);
sessionStorage.setItem("deadline", deadline);
}
initializeClock('clockdiv', deadline);





share|improve this answer


























  • Thank you very much but I do not know anything about sessionStorage if you can rewrite the code fully and I thank you again

    – mhmt
    Nov 21 '18 at 6:47






  • 1





    I'm not going to rewrite the whole thing when I'm just replacing two lines. I've updated the answer to explain exactly how this fits in.

    – Barmar
    Nov 21 '18 at 16:36






  • 1





    I included a link to the sessionStorage documentation. If that's not enough, I'm sure you can find tutorials using Google.

    – Barmar
    Nov 21 '18 at 16:37














1












1








1







Save deadline in sessionStorage.



Replace the two lines:



var deadline = new Date(Date.parse(new Date()) + 15 * 24 * 60 * 60 * 1000);
initializeClock('clockdiv', deadline);


with this:



var deadline;
var savedDeadline = sessionStorage.getItem("deadline");
if (savedDeadline) {
deadline = parseInt(savedDeadline);
} else {
deadline = new Date(Date.parse(new Date()) + 15 * 24 * 60 * 60 * 1000);
sessionStorage.setItem("deadline", deadline);
}
initializeClock('clockdiv', deadline);





share|improve this answer















Save deadline in sessionStorage.



Replace the two lines:



var deadline = new Date(Date.parse(new Date()) + 15 * 24 * 60 * 60 * 1000);
initializeClock('clockdiv', deadline);


with this:



var deadline;
var savedDeadline = sessionStorage.getItem("deadline");
if (savedDeadline) {
deadline = parseInt(savedDeadline);
} else {
deadline = new Date(Date.parse(new Date()) + 15 * 24 * 60 * 60 * 1000);
sessionStorage.setItem("deadline", deadline);
}
initializeClock('clockdiv', deadline);






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 21 '18 at 16:36

























answered Nov 20 '18 at 19:39









BarmarBarmar

424k35248349




424k35248349













  • Thank you very much but I do not know anything about sessionStorage if you can rewrite the code fully and I thank you again

    – mhmt
    Nov 21 '18 at 6:47






  • 1





    I'm not going to rewrite the whole thing when I'm just replacing two lines. I've updated the answer to explain exactly how this fits in.

    – Barmar
    Nov 21 '18 at 16:36






  • 1





    I included a link to the sessionStorage documentation. If that's not enough, I'm sure you can find tutorials using Google.

    – Barmar
    Nov 21 '18 at 16:37



















  • Thank you very much but I do not know anything about sessionStorage if you can rewrite the code fully and I thank you again

    – mhmt
    Nov 21 '18 at 6:47






  • 1





    I'm not going to rewrite the whole thing when I'm just replacing two lines. I've updated the answer to explain exactly how this fits in.

    – Barmar
    Nov 21 '18 at 16:36






  • 1





    I included a link to the sessionStorage documentation. If that's not enough, I'm sure you can find tutorials using Google.

    – Barmar
    Nov 21 '18 at 16:37

















Thank you very much but I do not know anything about sessionStorage if you can rewrite the code fully and I thank you again

– mhmt
Nov 21 '18 at 6:47





Thank you very much but I do not know anything about sessionStorage if you can rewrite the code fully and I thank you again

– mhmt
Nov 21 '18 at 6:47




1




1





I'm not going to rewrite the whole thing when I'm just replacing two lines. I've updated the answer to explain exactly how this fits in.

– Barmar
Nov 21 '18 at 16:36





I'm not going to rewrite the whole thing when I'm just replacing two lines. I've updated the answer to explain exactly how this fits in.

– Barmar
Nov 21 '18 at 16:36




1




1





I included a link to the sessionStorage documentation. If that's not enough, I'm sure you can find tutorials using Google.

– Barmar
Nov 21 '18 at 16:37





I included a link to the sessionStorage documentation. If that's not enough, I'm sure you can find tutorials using Google.

– Barmar
Nov 21 '18 at 16:37


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53399054%2ferror-when-i-refresh-count-down-its-restart%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

Npm cannot find a required file even through it is in the searched directory