views:

348

answers:

5

Which option is better to execute window.setTimeout and why?

Option A:

window.setTimeout(somefunc,0);

Option B:

window.setTimeout(somefunc,n); //n may be any number >0

Thanks.

+1  A: 

Option A will simply call somefunc with the additional overhead of needlessly calling setTimeout (since your second parameter means 0 milliseconds of delay). Option B is for if you intend to have a delay prior to the execution of somefunc. Could you elaborate a bit please, or does this answer your question?

karim79
I have few frames(1~6) and I make those frames editable using designmode="on". After that I set contents (from database) in those frames. But unfortunately, contents are not always loaded.
Hoque
+1  A: 

It depends on what you want to do.

setTimeout(somefunc,0) is not so different with just calling somefunc
(but I guess .setTimeout(somefunc,0) will finish current block first and then called somefunc)

If you need to wait browser rendering, and run somefunc after that, use window.onload or jQuery's $(document).ready

S.Mark
Instead of using somefunc() if I use setTimeout(somefunc,n) , then somefunc executes.It executes whether I put the delay "0" or any other value.
Hoque
@Hoque, the 2nd parameter just determines how long the "new thread" waits before firing somefunc(). If you don't "need" a new thread... just call somefunc()
scunliffe
Does not javascript work in a single thread? I am confused.
Hoque
JS is single threaded.
trinithis
true, it is single threaded... that's why "new thread" was in quotes. setTimeout and setInterval basically allow you run 2 or more bits of code simutaneously.
scunliffe
if single thread, then what will happen to the old thread? Will the old thread die and new thread will start?
Hoque
A: 
window.setTimeout(somefunc,0);

will run somefunc right away (but won't wait for a return value before continuing)

window.setTimeout(somefunc,n);

will wait n milliseconds before running somefunc (but won't wait for it to start or return before continuing)

or if you call somefunc() without the timeout it will run somefunc but wait for it to finish before continuing.

consider setTimeout to be like "starting" a new thread.

scunliffe
"won't wait for a return value", what is the return value. I am not clear about that. Would you mind to tell me more about the return value?
Hoque
any time you call a function it can return a value. e.g. `var name = getUserName();` could set `name` with a value returned from `getUserName()`. Even if you don't want or care to return a value... code after your call to `somefunc()` would not execute until somefunc was finished. By using setTimeout, you tell the browser you don't want to wait... continue executing code... and run somefunc() after `n` milliseconds.
scunliffe
So far I know that Javascript is single threaded. "consider setTimeout to be like "starting" a new thread." -then will it be multi-threaded?Thanks.
Hoque
+1  A: 

The thing about a timeout or an interval, they always wait for the current thread to run out before they execute their own function- even if you put it in the first line.

var color='white';
setTimeout(function(){alert(color+' from timeout')}, 0);
for(var i=0;i<100001;++i){
    if(i=100000)alert(color='green');
}
alert(color='red')
kennebec
Nice example, if I have two setTimeout, then which will be executed first?
Hoque
A: 

I recall having some browser issues when using setTimeout (func, 0) so now I do setTimeout (func, 1) whenever I want to have the shortest delay. Note that for most (all?) browsers the real shortest delay is some number n > 1 (definately n > 0 and probably n < 50).

Which browser I was having issues with I don't remember.

trinithis