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

Sleep

P: n/a
How can I suspend the execution of a script for x seconds?

Does exists something like sleep(x)?

Thanks.

------------------------------------
"Computer Science is no more about
computers than astronomy is about
telescopes."
::[E. W. Dijkstra]

Posted from X-Privat Free NNTP server - www.x-privat.org
Jul 20 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
You can use setTimeout, which calls a function after a set period of
time. If it's possible to separate out the task you wish to delay into
another function, this may be a solution.

function delay(){
alert('this happens now')
setTimeout('afterFiveSeconds()',5000)
}

function afterFiveSeconds(){
alert('this happens 5 seconds later')

}

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #2

P: n/a

"Xaradas" <no****@nospam.no> wrote in message news:3f******@x-privat.org...
How can I suspend the execution of a script for x seconds?

Does exists something like sleep(x)?

Thanks.

------------------------------------
"Computer Science is no more about
computers than astronomy is about
telescopes."
::[E. W. Dijkstra]

Posted from X-Privat Free NNTP server - www.x-privat.org


No, Just like in most languages, a blocking sleep is system dependent, and
not part of the language. The browser does not want you to do a blocking
sleep, because it will block the script from completing, thus blocking the
page from showing up. You can run setTimeout("function()", msDelay), but
that is a non-blocking delay... it will happen as an event.

There may be a way to get around this.... though the only way I can think of
off hand is a bit ugly, since it takes _ALL_ of the available processor,
which is BAD... but here is the thought:

var sleeping = true;
setTimeout("sleeping = false", 1000); // 1 second
while(sleeping) {}

The best way to have a delay, is to redesign your system to not require a
blocking delay, and use the non-blocking setTimeout.

Brian

Jul 20 '05 #3

P: n/a
"Brian" <Br**********@nospam.yahoo.com> wrote in message
news:3f********@10.10.0.241...
<snip>
There may be a way to get around this.... though the only way I
can think of off hand is a bit ugly, since it takes _ALL_ of
the available processor, which is BAD... but here is the thought:

var sleeping = true;
setTimeout("sleeping = false", 1000); // 1 second
while(sleeping) {}
No, that is not going to work as most browsers will not interrupt
running code in order to execute a setTimeout (indeed, I cannot think of
any browsers that would). So the while loop will keep consuming 100% of
processor power, and inhibiting most other activity in the browser,
until the page is unloaded (Or the browser puts up a "a script on this
page is causing the browser to run slowly ..." dialog.)
The best way to have a delay, is to redesign your system to not
require a blocking delay, and use the non-blocking setTimeout.


Absolutely, or have the rest of the code triggered by some other event.

Richard.
Jul 20 '05 #4

P: n/a
Xaradas wrote:
How can I suspend the execution of a script for x seconds?

Does exists something like sleep(x)?

Thanks.


In addition to the other responses, you can consider a design
such as:

function1() {
// do stuff
var t = setTimeout('function2();', your_delay);
}
function2() {
// do more stuff
var t = setTimeout('function3();', your_delay);
}
function function3() {
// etc
}

However, there is hardly ever a need to do this. If the problem
is that you are waiting for another frame or window to load,
doing something like will be *extremely* unreliable, since if you
set a long enough delay for the slowest of your client
connections, you will inevitably create a huge delay for your
fastest client connections.

As Richard has pointed out, JavaScript is event-driven. Have the
other window/frame send a signal to the opener or loader telling
you it loaded successfully.

If your problem is something else, perhaps expand on what problem
you are trying to solve and we can provide a more appropriate
solution.

--
| Grant Wagner <gw*****@agricoreunited.com>

* Client-side Javascript and Netscape 4 DOM Reference available
at:
*
http://devedge.netscape.com/library/...ce/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/a...ence_entry.asp

* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-deve...upgrade_2.html
Jul 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.