By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,483 Members | 1,039 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,483 IT Pros & Developers. It's quick & easy.

sleep function with a parameter

P: n/a
Hi all -

I'm trying to emulate a sleep function in javascript. I know of this
method for example:

window.setTimeout("function1();", 500);

However, I was wondering if there was a way to call the function with a
parameter, for example:

window.setTimeout("function1(string1);", 500);

This gives me a javascript error. Any help will be greatly
appreciated. Thanks in advance.

Oct 17 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Nevermind, it was a syntax error, it turns out you could just do

window.setTimeout(function1(string1), 500)

you dont need the quotes in the function declaration.

Oct 17 '05 #2

P: n/a

M B HONG 20 wrote:
Hi all -

I'm trying to emulate a sleep function in javascript. I know of this
method for example:

window.setTimeout("function1();", 500);

However, I was wondering if there was a way to call the function with a
parameter, for example:

window.setTimeout("function1(string1);", 500);

This gives me a javascript error. Any help will be greatly
appreciated. Thanks in advance.


Does function1 expect a string in its parameter? Then you should be
sending in a string:

window.setTimeout("function1('string1');", 500);

Oct 17 '05 #3

P: n/a
On 18/10/2005 00:43, M B HONG 20 wrote:
Nevermind, it was a syntax error, it turns out you could just do

window.setTimeout(function1(string1), 500)


That's something entirely different. Here you're calling the function,
function1, immediately. The return value from that call will be
converted to a string and evaluated approximately 500 milliseconds
later. This can be proven with:

function myFunction() {
alert('Called');
return '';
}

setTimeout(myFunction(), 10000);

No dialog box should appear for around ten seconds, yet it will appear
immediately.

I would guess that your identifier, string1, is local to some function.
As the setTimeout function is evaluated in global scope, an error would
occur with

setTimeout('function1(string1)', 500);

because when executed, string1 won't exist. To get around that,
concatenate the value of string1 into the literal, remembering to
include nested quotes:

setTimeout('function1("' + string1 + '");', 500);

If string1 contained the value, 'my string', the code above would call

function1("my string");

approximately 500ms later.

Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Oct 18 '05 #4

P: n/a
actually my mistake, the method

window.setTimeout(function1(string1), 500)

does not work, it does not actually sleep for the 500ms before firing
the function.

web.dev, thanks for the reply. Your method works for a straight string
passing through, but I need the function to be called with a variable
parameter.

Oct 18 '05 #5

P: n/a
Thanks a lot Michael, that did the trick.

Oct 18 '05 #6

P: n/a
M B HONG 20 wrote:
Hi all -

I'm trying to emulate a sleep function in javascript. I know of this
method for example:

window.setTimeout("function1();", 500);


After reading your previous posts in your thread I assume you wanted to
do something like
window.setTimeout(func, 500);
where func is a reference to a function, but also want the ability to
pass a parameter.

You can accomplish this using closures.
Example:

function displayMessage(message)
{
alert(message);
}

function setDelayedMessage()
{
var message = "hello";
var func = function()
{
displayMessage(message);
}
window.setTimeout(func, 500);
}

setDelayedMessage();
Oct 18 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.