View Full Version : sleep function in javascript

08-08-2006, 05:01 PM
Please tell me about sleep function in javascript


Philip M
08-08-2006, 06:44 PM
This is the setTimeout() method of the window object.
setTimeout("alert('message')", 5000); // the delay is expressed in milliseconds

Another example:-
var number=0;
function annoy() {
num ++;
alert (This is every ten-second message number: " + num);
window setTimeout("annoy()", 10000); // every ten seconds
annoy ();

Google will point you to many further explanations and examples, e.g.

08-09-2006, 02:28 AM
Note that setTimeout is an asynchronous call. Any following Javascript will be immediately run. Only the function called by the setTimeout will be delayed.

Javascript has no equivalent to the sleep function found in most other languages.

08-09-2006, 05:21 AM

I making call to function of Ajax script. This runs parallel.
Sometimes the function doesnot get executed fully.

That's why I need sleep.

Please suggest some method.


08-09-2006, 06:50 AM
Well, instead of using request.open("uri", "GET", true) you could use request.open("uri", "GET", false) to make it a synchronous call and have the scripting environment wait until the document is received.

08-10-2006, 06:52 AM
Thanks very much,
This reallly worked.
My problem has got solved.

08-18-2006, 08:33 AM
I changed my code to XmlHttp.open("GET", requestUrl, false);
so the script becomes synchronous.

It works fine in IE but not in Netscape

The part of code is


//Setting the event handler for the response
XmlHttp.onreadystatechange = HandleResponse;

//Initializes the request object with GET (METHOD of posting),
//Request URL and sets the request as asynchronous.
XmlHttp.open("GET", requestUrl, false);

//Sends the request to server
On making it synchronous HandleResponse function does not get triggered.

Please suggest some method to trigger that function by keeping the script synchronous

08-18-2006, 01:12 PM
Why would you need the readystatechange handler if the call is synchronous?

//processing waits until the request has completed
alert(xmlhttp.responseText); // this works fine!

08-23-2006, 05:29 AM
Thank for the solution.

Actually I had figured it myself as well.

10-09-2008, 06:06 PM
I hav a same problem.
when i use the XmlHttp.open("GET", requestUrl, false);
the call back function not be called in firefox and safari.
please provide the solution.


10-09-2008, 06:09 PM
I have a same kind of problem.when i use the XmlHttp.open("GET", requestUrl, false);
statement,the callback function not be called in firefox and safari.
please provide the solution.

Thanks .

rnd me
10-09-2008, 09:42 PM
Why would you need the readystatechange handler if the call is synchronous?

there is a bug, thats why. some firefoxes doesn't like callbacks from synchronous ajax.

you can really forgo the callback, or simply manually call it after xhr.send.

probably easier to simply move the code from the onreadystatechange function to the parent codeblock, right after .send().

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum