Refresh page after clearInterval execution
up vote
1
down vote
favorite
I have some trivial code like the following:
var timer_ends = some_ts;
var timer = setInterval(function()
time_diff = /* some calculation */
if ( time_diff <= 0 )
clearInterval(timer)
// Want to location.reload(true) here but it would recursively run
return;
, 1000);
Once the timer hits 0 or below, I want to
- clearInterval(timer)
- I want to refresh the page
The problem is, if I refresh the page, the timer will just restart and we'll recursively keep refreshing. What is the best way to execute the location.reload(true)
once and only once the first time time_diff
hits 0. Also, if the user is coming to the page for the first time and it's already at 0 it shouldn't refresh.
javascript
|
show 4 more comments
up vote
1
down vote
favorite
I have some trivial code like the following:
var timer_ends = some_ts;
var timer = setInterval(function()
time_diff = /* some calculation */
if ( time_diff <= 0 )
clearInterval(timer)
// Want to location.reload(true) here but it would recursively run
return;
, 1000);
Once the timer hits 0 or below, I want to
- clearInterval(timer)
- I want to refresh the page
The problem is, if I refresh the page, the timer will just restart and we'll recursively keep refreshing. What is the best way to execute the location.reload(true)
once and only once the first time time_diff
hits 0. Also, if the user is coming to the page for the first time and it's already at 0 it shouldn't refresh.
javascript
1
Have you tried using cookies?
– TheMintyMate
Nov 9 at 19:24
can also use localStorage if server is not involved in the timing or tracking
– charlietfl
Nov 9 at 19:26
What is local storage?
– Electrox Mortem
Nov 9 at 19:30
@JermahlWhite plese see docs here Window localStorage Property
– Tornike Shavishvili
Nov 9 at 19:34
Why do you want to refresh at all?
– Jonas Wilms
Nov 9 at 19:34
|
show 4 more comments
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have some trivial code like the following:
var timer_ends = some_ts;
var timer = setInterval(function()
time_diff = /* some calculation */
if ( time_diff <= 0 )
clearInterval(timer)
// Want to location.reload(true) here but it would recursively run
return;
, 1000);
Once the timer hits 0 or below, I want to
- clearInterval(timer)
- I want to refresh the page
The problem is, if I refresh the page, the timer will just restart and we'll recursively keep refreshing. What is the best way to execute the location.reload(true)
once and only once the first time time_diff
hits 0. Also, if the user is coming to the page for the first time and it's already at 0 it shouldn't refresh.
javascript
I have some trivial code like the following:
var timer_ends = some_ts;
var timer = setInterval(function()
time_diff = /* some calculation */
if ( time_diff <= 0 )
clearInterval(timer)
// Want to location.reload(true) here but it would recursively run
return;
, 1000);
Once the timer hits 0 or below, I want to
- clearInterval(timer)
- I want to refresh the page
The problem is, if I refresh the page, the timer will just restart and we'll recursively keep refreshing. What is the best way to execute the location.reload(true)
once and only once the first time time_diff
hits 0. Also, if the user is coming to the page for the first time and it's already at 0 it shouldn't refresh.
javascript
javascript
asked Nov 9 at 19:22
randombits
11k54177348
11k54177348
1
Have you tried using cookies?
– TheMintyMate
Nov 9 at 19:24
can also use localStorage if server is not involved in the timing or tracking
– charlietfl
Nov 9 at 19:26
What is local storage?
– Electrox Mortem
Nov 9 at 19:30
@JermahlWhite plese see docs here Window localStorage Property
– Tornike Shavishvili
Nov 9 at 19:34
Why do you want to refresh at all?
– Jonas Wilms
Nov 9 at 19:34
|
show 4 more comments
1
Have you tried using cookies?
– TheMintyMate
Nov 9 at 19:24
can also use localStorage if server is not involved in the timing or tracking
– charlietfl
Nov 9 at 19:26
What is local storage?
– Electrox Mortem
Nov 9 at 19:30
@JermahlWhite plese see docs here Window localStorage Property
– Tornike Shavishvili
Nov 9 at 19:34
Why do you want to refresh at all?
– Jonas Wilms
Nov 9 at 19:34
1
1
Have you tried using cookies?
– TheMintyMate
Nov 9 at 19:24
Have you tried using cookies?
– TheMintyMate
Nov 9 at 19:24
can also use localStorage if server is not involved in the timing or tracking
– charlietfl
Nov 9 at 19:26
can also use localStorage if server is not involved in the timing or tracking
– charlietfl
Nov 9 at 19:26
What is local storage?
– Electrox Mortem
Nov 9 at 19:30
What is local storage?
– Electrox Mortem
Nov 9 at 19:30
@JermahlWhite plese see docs here Window localStorage Property
– Tornike Shavishvili
Nov 9 at 19:34
@JermahlWhite plese see docs here Window localStorage Property
– Tornike Shavishvili
Nov 9 at 19:34
Why do you want to refresh at all?
– Jonas Wilms
Nov 9 at 19:34
Why do you want to refresh at all?
– Jonas Wilms
Nov 9 at 19:34
|
show 4 more comments
2 Answers
2
active
oldest
votes
up vote
1
down vote
var timer_ends = some_ts;
if (Boolean(localStorage.isEnteredBefore)) return;
var timer = setInterval(function()
time_diff = /* some calculation */
if ( time_diff <= 0 )
clearInterval(timer)
// Want to location.reload(true) here but it would recursively run
localStorage.isEnteredBefore = true;
return;
, 1000);
Assuming that you are at the function level.
– Ertan Kara
Nov 9 at 19:32
add a comment |
up vote
1
down vote
The code for the cookies is from W3Schools. This should be what you want
function setCookie(cname, cvalue, exdays)
var d = new Date();
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
var expires = "expires="+ d.toUTCString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
function getCookie(cname)
var name = cname + "=";
var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie.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 "";
var timer_ends = some_ts;
var timer = setInterval(function()
time_diff = /* some calculation */
if ( time_diff <= 0 )
clearInterval(timer)
if(getCookie('done')) return
setCookie('done', true, 30)
location.reload(true)
//No need to return here because it's already reloading
, 1000);
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
var timer_ends = some_ts;
if (Boolean(localStorage.isEnteredBefore)) return;
var timer = setInterval(function()
time_diff = /* some calculation */
if ( time_diff <= 0 )
clearInterval(timer)
// Want to location.reload(true) here but it would recursively run
localStorage.isEnteredBefore = true;
return;
, 1000);
Assuming that you are at the function level.
– Ertan Kara
Nov 9 at 19:32
add a comment |
up vote
1
down vote
var timer_ends = some_ts;
if (Boolean(localStorage.isEnteredBefore)) return;
var timer = setInterval(function()
time_diff = /* some calculation */
if ( time_diff <= 0 )
clearInterval(timer)
// Want to location.reload(true) here but it would recursively run
localStorage.isEnteredBefore = true;
return;
, 1000);
Assuming that you are at the function level.
– Ertan Kara
Nov 9 at 19:32
add a comment |
up vote
1
down vote
up vote
1
down vote
var timer_ends = some_ts;
if (Boolean(localStorage.isEnteredBefore)) return;
var timer = setInterval(function()
time_diff = /* some calculation */
if ( time_diff <= 0 )
clearInterval(timer)
// Want to location.reload(true) here but it would recursively run
localStorage.isEnteredBefore = true;
return;
, 1000);
var timer_ends = some_ts;
if (Boolean(localStorage.isEnteredBefore)) return;
var timer = setInterval(function()
time_diff = /* some calculation */
if ( time_diff <= 0 )
clearInterval(timer)
// Want to location.reload(true) here but it would recursively run
localStorage.isEnteredBefore = true;
return;
, 1000);
answered Nov 9 at 19:30
Ertan Kara
707
707
Assuming that you are at the function level.
– Ertan Kara
Nov 9 at 19:32
add a comment |
Assuming that you are at the function level.
– Ertan Kara
Nov 9 at 19:32
Assuming that you are at the function level.
– Ertan Kara
Nov 9 at 19:32
Assuming that you are at the function level.
– Ertan Kara
Nov 9 at 19:32
add a comment |
up vote
1
down vote
The code for the cookies is from W3Schools. This should be what you want
function setCookie(cname, cvalue, exdays)
var d = new Date();
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
var expires = "expires="+ d.toUTCString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
function getCookie(cname)
var name = cname + "=";
var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie.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 "";
var timer_ends = some_ts;
var timer = setInterval(function()
time_diff = /* some calculation */
if ( time_diff <= 0 )
clearInterval(timer)
if(getCookie('done')) return
setCookie('done', true, 30)
location.reload(true)
//No need to return here because it's already reloading
, 1000);
add a comment |
up vote
1
down vote
The code for the cookies is from W3Schools. This should be what you want
function setCookie(cname, cvalue, exdays)
var d = new Date();
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
var expires = "expires="+ d.toUTCString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
function getCookie(cname)
var name = cname + "=";
var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie.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 "";
var timer_ends = some_ts;
var timer = setInterval(function()
time_diff = /* some calculation */
if ( time_diff <= 0 )
clearInterval(timer)
if(getCookie('done')) return
setCookie('done', true, 30)
location.reload(true)
//No need to return here because it's already reloading
, 1000);
add a comment |
up vote
1
down vote
up vote
1
down vote
The code for the cookies is from W3Schools. This should be what you want
function setCookie(cname, cvalue, exdays)
var d = new Date();
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
var expires = "expires="+ d.toUTCString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
function getCookie(cname)
var name = cname + "=";
var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie.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 "";
var timer_ends = some_ts;
var timer = setInterval(function()
time_diff = /* some calculation */
if ( time_diff <= 0 )
clearInterval(timer)
if(getCookie('done')) return
setCookie('done', true, 30)
location.reload(true)
//No need to return here because it's already reloading
, 1000);
The code for the cookies is from W3Schools. This should be what you want
function setCookie(cname, cvalue, exdays)
var d = new Date();
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
var expires = "expires="+ d.toUTCString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
function getCookie(cname)
var name = cname + "=";
var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie.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 "";
var timer_ends = some_ts;
var timer = setInterval(function()
time_diff = /* some calculation */
if ( time_diff <= 0 )
clearInterval(timer)
if(getCookie('done')) return
setCookie('done', true, 30)
location.reload(true)
//No need to return here because it's already reloading
, 1000);
answered Nov 9 at 19:30
Electrox Mortem
14713
14713
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53232087%2frefresh-page-after-clearinterval-execution%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
1
Have you tried using cookies?
– TheMintyMate
Nov 9 at 19:24
can also use localStorage if server is not involved in the timing or tracking
– charlietfl
Nov 9 at 19:26
What is local storage?
– Electrox Mortem
Nov 9 at 19:30
@JermahlWhite plese see docs here Window localStorage Property
– Tornike Shavishvili
Nov 9 at 19:34
Why do you want to refresh at all?
– Jonas Wilms
Nov 9 at 19:34