Nick
Nick

Reputation: 7525

setTimeout in javascript question

I understand that the setTimeout function spawns a separate thread that waits for x milliseconds before executing the JS function

setTimeout(functionName, timeInms);

My question is if there's a way to tell it to run after the JS on the page has completed executing? Since this depends on how much JS is on the page, this cannot be an absolute number.

This is a part of a bigger problem I have that I had posted here:

link text

The gist is that ScriptManager does not guarantee the order of execution and I have to run the EndScript function at the very end. It kinda works with the setTimeOut although it's not very accurate (since the JS on the pages differ)

Upvotes: 1

Views: 881

Answers (1)

SLaks
SLaks

Reputation: 888107

Javascript is single-threaded.

If you set a timer that expires while Javascript code is still running, it will wait for the code to finish before running the timer function.

EDIT: To clarify, it will wait until the timer "rings", then execute the function as soon as the JS thread is free, which means after any Javascript code finishes.

Note that it will wait for all of the code to finish running, not just your particular file.

Upvotes: 7

Related Questions