473,756 Members | 2,383 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Sleep

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
4 68016
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('aft erFiveSeconds() ',5000)
}

function afterFiveSecond s(){
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

"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("fun ction()", 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("sle eping = 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
"Brian" <Br**********@n ospam.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("sle eping = 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
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('fun ction2();', your_delay);
}
function2() {
// do more stuff
var t = setTimeout('fun ction3();', 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*****@agrico reunited.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
13979
by: Anks | last post by:
hi I am displaying a digital clock in an applet.I am using Thread.sleep (1000) for counting every second.This works fine some times but some times my clock just slows down or give an interrupted exception .I am not calling Thread.interrupt anywhere in my program. why is it happening? how to fix the problem? Anks
7
10298
by: Colin Brown | last post by:
Python 2.3.3 Under Windows NT: Negative Int or Real -> sleeps for a long time Under Linux: Returns IOError: Invalid argument I consider both of these unfriendly results and expected the preferable silent time.sleep(0) in much the same way that slicing a string beyond the ends returns an empty string. Colin Brown PyNZ
5
5538
by: Dave | last post by:
For the life of me, I thought there was a sleep() function in the Standard Library (or something similarly named), but I can't find it. Is it non-existent or, if not, what header is it in? Thanks! Dave
21
18750
by: Alo Sarv | last post by:
Hi From what I have understood from various posts in this newsgroup, writing event loops pretty much comes down to this: while (true) { handleEvents(); sleep(1); // or _sleep() or nanosleep(), depending on platform }
8
2785
by: Cider123 | last post by:
I ran into a situation where my Window Service had to process 100,000+ files, when I first noticed I needed to tweak various routines. Everything runs fine, but here's what I ran into: In the routine that loops through the file buffer: for (int i=0;i < _Buffer.length; i++) { // Code here
9
3486
by: Chris Dunaway | last post by:
According to the docs, calling Thread.Sleep(0) causes the thread to be "suspended to allow other waiting threads to execute." What happens if I call Thread.Sleep(500)? Do other threads not get a chance to execute during this time? What is the difference between the two? I have code that runs in a loop like this: Dim dResetTime As DateTime = DateTime.Now
17
6431
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. Note, the Windows task manager shows 2 CPUs on the Performance tab with hyper-threading is turned on. Both Python 2.3.5 and 2.4.3 (downloaded from python.org) have this problem. The operating system is MS Windows XP Professional.
1
2774
by: Matt Brown - identify | last post by:
Hello, I, as well as suspecting many others, am having an issue when I attempt to enter sleep mode in Vista. If any program is connected to the internet (AOL Instant Messenger for instance), and I go to enter Sleep mode; Vista will automatically leave Sleep mode immediately. I believe this is caused by the fact that Sleep attempts to disconnect AIM, then AIM attempts to reconnect; thrwarting Vista's attempt to
13
5836
by: Charles Zhang | last post by:
Sleep() function Sleep at lease 1 millisecond, there is a way to make a thread to sleep less than a millisecond? One way I know of is using performance counter which is not really sleep ( loop and check again and again and uses CPU time). Thanks Charles Zhang
0
1403
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 reproduces your problem is: <code> import signal
0
9275
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10034
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9872
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9713
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8713
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7248
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6534
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5142
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3805
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.