Connecting Tech Pros Worldwide Help | Site Map

whats wrong with this wait function?

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 20th, 2005, 11:23 AM
Rasmus Grøndahl Olsen
Guest
 
Posts: n/a
Default 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);
}
}



  #2  
Old July 20th, 2005, 11:23 AM
Lasse Reichstein Nielsen
Guest
 
Posts: n/a
Default Re: whats wrong with this wait function?

"Rasmus Grøndahl Olsen" <zordid@bigfoot.com> writes:
[color=blue]
> 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?[/color]

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.
[color=blue]
> function wait(time) {
> while(1){
> setTimeout("break;",time);
> }
> }[/color]

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.
[color=blue]
> function wait(time) {
> var flag=0;
> while(flag=0){
> setTimeout("flag=1;",time);
> }
> }[/color]

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 - lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
  #3  
Old July 20th, 2005, 11:23 AM
Evertjan.
Guest
 
Posts: n/a
Default Re: whats wrong with this wait function?

Rasmus Grøndahl Olsen wrote on 27 okt 2003 in comp.lang.javascript:
[color=blue]
> 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);
> }
>}
>[/color]

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)
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.