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

Q1272 Can I invoke a function with parameters using setTimeout?

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

The setTimeout method takes an expression to be evaluted after the interval has expired. The expression is commonly used by way of a string - but it doesn't have to be - it can be anything that evaluates to some string that can then be passed to the setTimeout method.

So if we use:

setTimeout(functionA(parms),7000);
//--></script>

It executes functionB() now! Passing the parms to functionB() now! And then passes its result as input to the setTimeout method which is then evaluted in 7 seconds time.

Note: if the functionA() returns nothing then the JavaScript code will throw an error.

If using:

setTimeout('functionA(parms)',7000);
//--></script>

We pass a string 'funtionB(parms)' as input to the setTimeout method which is evaluated in 7 seconds time. If parms no longer have scope within this context, or functionA() no longer exists then an error occurs.

Feedback on 'Q1272 Can I invoke a function with parameters using setTimeout?'

©2018 Martin Webb