Home Articles FAQs XREF Games Software Instant Books About Feedback Search Site-Map
irt.org logo

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

irt.org | Knowledge Base | JavaScript | Timeout | Q946 [ previous next ]

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

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>

Provide feedback ...
AddThis Social Bookmark Button

Provide feedback ... AddThis Social Bookmark Button


Last Updated: 6th July 2009. Maintained by: Martin Webb and Michel Plungjan
irt.org liability, trademark, document use, privacy statement and software licensing rules apply.
Copyright © 1996-2009 irt.org, All Rights Reserved.