473,396 Members | 1,797 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,396 software developers and data experts.

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("break;",time);
}
}

function wait(time) {
var flag=0;
while(flag=0){
setTimeout("flag=1;",time);
}
}
Jul 20 '05 #1
2 11411
"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("break;",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("flag=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("flag = true",time)
while(!flag){};
}

or

function wait(time) {
var flag = false;
setTimeout(function(){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(function(){
// 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/rasterTriangleDOM.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.javascript:
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("break;",time);
}
}

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


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

try this:

<script>

function main() {
// do this
// do that
setTimeout("main2()",1000); // 1 second
}
function main2() {
// do this
// do that
setTimeout("main3()",2000); // 2 seconds
}
function main3() {
// do this
// do that
setTimeout("main4()",60000); // 1 minute
}
function main4() {
// do this
// do that
setTimeout("main2()",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
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)...
2
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>...
6
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
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...
7
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...
5
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
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...
5
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...
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?
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
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...
0
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...
0
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...
0
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,...

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.