Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org
#

Q946 Why is the first parameter of the setTimeout() method enclosed in quotes?

You are here: irt.org | FAQ | JavaScript | Timeout | Q946 [ previous next ]

Its an expression to be evaluated at the time of the execution.

If you did:

setTimeout(test(),5000);

Then the test() function will be evaulated straight away and the result returned as the expression to be evaluated after a five second delay, which is probably not want you want.

But this could be useful, for example:

<script language="JavaScript"><!--
function test1() {
    alert('test1() function invoked immeadiately');
    return 'test2()';
}

function test2() {
    alert('test2() function invoked five seconds later');
}

setTimeout(test1(),5000);
//--></script>

©2018 Martin Webb