473,785 Members | 2,767 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

whats wrong with this wait function?

I have tried to write a wait function but it seems like it will not brake
the while loop. I tried two different solutions.
Can anyone tell me what I am doing wrong, and come with another suggestion?
If I call the function like this: wait(500); it should wait 500ms right?

function wait(time) {
while(1){
setTimeout("bre ak;",time);
}
}

function wait(time) {
var flag=0;
while(flag=0){
setTimeout("fla g=1;",time);
}
}
Jul 20 '05 #1
2 11435
"Rasmus Grøndahl Olsen" <zo****@bigfoot .com> writes:
I have tried to write a wait function but it seems like it will not brake
the while loop. I tried two different solutions.
Can anyone tell me what I am doing wrong, and come with another suggestion?
If I call the function like this: wait(500); it should wait 500ms right?
No.

setTimeout doesn't dealy execution. It schedules the argument code to
be executed at a later time, and then continues with the current code.
function wait(time) {
while(1){
setTimeout("bre ak;",time);
}
}
The code "break" is not executed as part of the current context, but as
fresh code in the global context. It has no way of breaking a while
loop that it is not inside.
function wait(time) {
var flag=0;
while(flag=0){
setTimeout("fla g=1;",time);
}
}


This could work, except that the flag variable is local, and the
scheduled code is executed in a different context.

No need to make more than one setTimeout.

Try either:

var flag = false;
function wait(time) {
setTimeout("fla g = true",time)
while(!flag){};
}

or

function wait(time) {
var flag = false;
setTimeout(func tion(){flag = true;},time);
while(!flag){};
}

In both cases, you spend all available processor power on a busy loop
that does nothing. That (called "busy waiting") is bad style in any
language.

Instead of using a wait function, you should reschedule the remainder
of your code instead.

// blah blah code
wait(500);
// blah blah more code

should be
// blah blah code
setTimeout(func tion(){
// blah blah more code
},500);

Ofcourse, if you plan on returning a result, it will need more fixing.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2
Rasmus Grøndahl Olsen wrote on 27 okt 2003 in comp.lang.javas cript:
I have tried to write a wait function but it seems like it will not
brake the while loop. I tried two different solutions.
Can anyone tell me what I am doing wrong, and come with another
suggestion? If I call the function like this: wait(500); it should
wait 500ms right?

function wait(time) {
while(1){
setTimeout("bre ak;",time);
}
}

function wait(time) {
var flag=0;
while(flag=0){
setTimeout("fla g=1;",time);
}
}


That is not the way JS works, even with while(flag==0)

try this:

<script>

function main() {
// do this
// do that
setTimeout("mai n2()",1000); // 1 second
}
function main2() {
// do this
// do that
setTimeout("mai n3()",2000); // 2 seconds
}
function main3() {
// do this
// do that
setTimeout("mai n4()",60000); // 1 minute
}
function main4() {
// do this
// do that
setTimeout("mai n2()",2000); // 2 seconds, then loop to main2
}

main()

</script>

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jul 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
3471
by: Colin Steadman | last post by:
I have created a function to kill all session variables that aren't in a safe list. This is the function - Sub PurgeSessionVariables For Each Item In Session.Contents Select Case Trim(Item) Case "Authenticated" Case "CI_CODE" Case "organisation_description" Case "location_description"
2
1212
by: Robert Smith | last post by:
Why doesnt this code work???? it just stops at the socket() and does not print HI. why is this? #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/socket.h> #include <arpa/inet.h> #include <netinet/in.h>
6
3117
by: thesushant | last post by:
hi, whats the use of third argument to main( ), i.e. environmental parameters.... if i am not wrong ? 1st 1 is argc 2nd is argv and what bout the 3rd 1??????????? sushant
5
2835
by: kernel.lover | last post by:
hello, I want to know if a fuction say malloc is declared as void *malloc() then whats the significance of void here. Does void * is used when function has the flexibility to return any type of value by casting that functions with appropriate data type?
7
2231
by: Mike Barnard | last post by:
It's a simple test... VERY SIMPLE. But... In an external stlyesheet some attributes don't show. With the same styles cut and pasted to the test internally it works as expected. Anyone tell me why? Its probably sooooooo obvious, but it is 1.19 am! Thanks. www.thunderin.co.uk/
5
4621
by: Jim Langston | last post by:
Expected output of this program is: 1 -1 -1 -1 Using Microsoft Visual C++ .net 2003 actual output is: 1 4294967295 4294967295
10
2368
by: joestevens232 | last post by:
Ok everything is good...I read in all my grades into each of the different arrays that they need to go in. Now Im trying to write a function that takes the grades and computes the average on a hundred point scale...for example: Student 1 7 quiz scores 9 9.33 8 10 5.5 8 10 ==> the 5.5 score should be dropped, returned value: 90.55 6 project scores 20 47.5 47 45 47.5 48 ==> returned value: 94.44 2 exam scores 83 87 ==>...
5
1723
by: hiqu | last post by:
This issue is driving me nuts and not able to figure out whats wrong. I've this code in my firefox extension. Firefox always hangs and reports the script is busy. if I introduce a break statement in the for(;;) loop below, then no issue. Any help would be appreciated! function getStuff()
0
9645
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
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
10329
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
10152
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...
1
10092
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9950
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...
1
7500
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...
1
4053
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
3
2880
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.