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.










share|improve this question

















  • 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














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.










share|improve this question

















  • 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












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.










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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












  • 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












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);





share|improve this answer




















  • Assuming that you are at the function level.
    – Ertan Kara
    Nov 9 at 19:32

















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);





share|improve this answer




















    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',
    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%2f53232087%2frefresh-page-after-clearinterval-execution%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    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);





    share|improve this answer




















    • Assuming that you are at the function level.
      – Ertan Kara
      Nov 9 at 19:32














    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);





    share|improve this answer




















    • Assuming that you are at the function level.
      – Ertan Kara
      Nov 9 at 19:32












    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);





    share|improve this answer












     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);






    share|improve this answer












    share|improve this answer



    share|improve this answer










    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
















    • 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












    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);





    share|improve this answer
























      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);





      share|improve this answer






















        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);





        share|improve this answer












        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);






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 9 at 19:30









        Electrox Mortem

        14713




        14713



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            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





















































            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

            Use pre created SQLite database for Android project in kotlin

            Darth Vader #20

            Ondo