JavaScript Settimeout bug

I have some JavaScript code that looks like:

    <script>
      // For todays date;
      var today = new Date();
      var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
      var n = time;
      var start = '6:56:00'
      var end   = '7:09:59'

      setTimeout(function run(){
                console.log()
                if (n >= start && n < end) 
                {
                  window.location.href = "localhost/template/script2.html";
                } else
                {
                  location.reload(true);
                }
            
            console.log(run, 1000);
    
      }, 1000);
    </script>

I want the program to run only at 6:56:00 - 7:09:59

but it runs at 6:05:00 - 6:19:59 and 6:56:00 - 7:09:59 What should I do?



Read more here: https://stackoverflow.com/questions/64412084/javascript-settimeout-bug

Content Attribution

This content was originally published by SDG at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: