473,386 Members | 1,973 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

sleep function with a parameter

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
6 11168
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

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
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
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
Thanks a lot Michael, that did the trick.

Oct 18 '05 #6
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: K Balusu | last post by:
Hi all, We have to develop a small engine which the client uses. It supposed to work like this. Our engine resides in a frame (frameA) which will be loaded only once and it provides set of...
10
by: Alfonso Morra | last post by:
Hi, I need help witht he sleep function as follows. I need to be write som code to do the ff: 1. creates a new thread 2. (in the new thread), Sleep for x milliseconds 3. (in the new thread),...
5
by: Sinan Nalkaya | last post by:
hello, i need a function like that, wait 5 seconds: (during wait) do the function but function waits for keyboard input so if you dont enter any it waits forever. i tried time.sleep() but when...
17
by: OlafMeding | last post by:
Below are 2 files that isolate the problem. Note, both programs hang (stop responding) with hyper-threading turned on (a BIOS setting), but work as expected with hyper-threading turned off. ...
2
by: fltcpt | last post by:
After reading the many posts on the newsgroup, I still disagree with the people who claim you will never need a sleep(), or that a sleep() is a bad idea, or that sleep() does not fit in the event...
2
by: The Last Ottoman | last post by:
Hi, I'm a student in Turkey... I used sleep() function in my second project, and I included <windows.h>, <time.h> and <iostream>... then, the function worked properly... After a while, my...
12
by: Morten Snedker | last post by:
I open a file for input. Each line is handled individually. Every time a line has been handled I wish to wait for ½ second before reading the next. Am I doing it right or wrong ? (I think it's...
6
by: Darth | last post by:
Hi, How does one make a thread sleep for some "microseconds" in C# ? TIA, Darth
0
by: Tim Golden | last post by:
Lowell Alleman wrote: Well you've certainly picked a ticklish area to run into problems with ;). First, forget about the threading aspects for the moment. AFAICT the smallest program which...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.