0

I want a website to refresh every 1 second, but I also want the javascript codes above the setTimeout function to execute first before the setTimeout function executes. If I put the setTimeout function below the codes, does it mean that it will only execute the setTimeout function once the codes above are executed?

//long code that i want executed before the setTimeout executes.

var timeout = setTimeout("location.reload(true);",1000);
function resetTimeout() {
    clearTimeout(timeout);
    timeout = setTimeout("location.reload(true);",1000);
}

So basically I want this to happen. The codes above executes. Then and only then will the setTimeout function execute - pausing for a second before refreshing the website.

Phil
  • 157,677
  • 23
  • 242
  • 245
frosty
  • 2,559
  • 8
  • 37
  • 73
  • 1
    Yes, that is how `setTimeout` works. You probably could have run your code to find out – Phil Aug 22 '18 at 02:06
  • 1
    ... if the code above is synchronous. – Kaiido Aug 22 '18 at 02:13
  • I would Suggest `setInterval()` as you don't have to keep calling `setTimeout()` just put code before setInterval and it will run first – Industrial Comet Aug 22 '18 at 02:19
  • 2
    @IndustrialComet why an interval since the code that is executed in the callback is `location.reload(true)`. You will always have only one occurence of your interval to fire. – Kaiido Aug 22 '18 at 02:21

0 Answers0