Wednesday, November 21, 2007

Javascript : tricky thing for setTimeout

I have this:

function callme() {
alert('can you hear me?');
}

What's the difference among these calls?
1) setTimeout(alert('can you hear me?'), 1000);
2) setTimeout(callme(), 1000);
3) setTimeout(function () { callme(); }, 1000);

1) and 2) are the same, callme() will be executed when setTimeout processing its parameter.
3) will be called after 1000ms, likely asynchronized.

---
Roger says:
"the arguments are evaluated when setTimeout is called, so is that init function, what might work is just passing callme which is an object.

the latter (case 2) preserves the variables so to speak... scope, context"

Firefox3 has a problem.

"xul-overlay-merged observer is notified before script elements within the overlay are processed"

https://bugzilla.mozilla.org/show_bug.cgi?id=392515

Well, I guess this could be a work around.

In the <overlay id=...>, we should have <prefpane id=... onpaneload="myInit();">.
So after something like '<script type="application/x-javascript" src="chrome://..." />, 

we use

    <script type="application/x-javascript">
    <![CDATA[
    // UI-specific code
        setTimeout(function () {/*whatever*/},100);
    ]]>
    </script>

Just the onpaneload has no use here.