I require a timer on my site. This timer will run a series of web services when it expires.

How can I prevent my timer from starting again on a page refresh or page load? I assume I'll need some PHP code here?

I thought of using sessions but I can't update my session on my static page as my JS timer runs.

    Without knowing all the specifics you could investigate these ideas:

    1) Use local storage. When the page loads, have JavaScript check if a certain value or flag is set, etc., and act accordingly. Maybe when the timer begins you set a flag. When it expires, the flag is removed, or value changed.

    2) As the timer runs, make an AJAX call (or several) to a PHP script that updates a session variable. When the page loads, check if the variable exists and for its value (maybe some kind of flag).

    Hope these ideas help!

      1. How would you use local storage?
      2. this works I guess if I had my JS timer doing something ever 5 seconds and I called an AJAX script then. On my 5 minute timer I would need count each 5 second and say after 36 instances of 5 seconds I'm at 5 minutes. Do something else. This would call my AJAX page a large volume of times on a very frequent basis. Is there any issue with that?

      <button onclick="setTimeout(myFunction, 5000)">Try it</button>

        NZ_Kiwis;11052751 wrote:

        1. How would you use local storage?

        myStorage = localStorage;
        
        myStorage.foo = "Kiwis";
        
        console.log(myStorage.foo);
        Mozilla Developer Network wrote:

        The localStorage property allows you to access a local Storage object. localStorage is similar to sessionStorage. The only difference is that, while data stored in localStorage has no expiration time, data stored in sessionStorage gets cleared when the browsing session ends—that is, when the browser is closed.

        https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage

          NZ_Kiwis;11052751 wrote:

          2. this works I guess if I had my JS timer doing something ever 5 seconds and I called an AJAX script then. On my 5 minute timer I would need count each 5 second and say after 36 instances of 5 seconds I'm at 5 minutes. Do something else. This would call my AJAX page a large volume of times on a very frequent basis. Is there any issue with that?

          Rather than counting how many times a 5-second interval has run (which might also get reset if the page were refreshed) I would instead store some kind of absolute time marker. E.g., you might store a timestamp somewhere:

          // note in Javascript this will be the current timestamp in milliseconds, not seconds
          var startTimeStamp = Date.now();

          Then your function that runs every 1 second or five seconds would compare the stored timestamp against the current time and if the difference is greater than five minutes, then run the routine. If you store startTimeStamp either on the server in your db or in a cookie or some kind of persistent storage (like localStorage) then the startTimeStamp value will be preserved even if you refresh the page. Your logic might need to update the startTimeStamp value when your process runs if you want this sort of thing to run every five minutes.

            Write a Reply...