So we all know setTimeout waits a certain amount of time before executing something. My question is, does it wait for the above code to finish executing first, before waitin
In your case, the page will reload 1 second after setTimeout
was called. So it's the "huge chunk of code" time plus 1 second.
To refresh the page as soon as the first part of the code finishes, just call location.reload
without setTimeout
:
if (1) {
//huge chunk of code
}
location.reload(true);
EDIT: This approach doesn't wait for asynchronous code to finish. For example, the program below is interrupted by the reload before the alert box pops up.
if (1) {
setTimeout(() => alert('Test'), 1000);
}
location.reload(true);