...

View Full Version : Exiting a loop based on two different conditions



isjhe
05-14-2009, 10:21 PM
I'm checking a variable to see if it holds a specific value. If it doesn't hold the value, I need to wait a few ms, and then check again. If at any time the var holds the correct value, I need to exit the loop and do something else with that var. I also want to exit the loop if the var test is false more than 5 times, at this point I would like to do something else (error message).

I figured out how to loop until the var holds the right value, and I can loop for X amount of times, but I'm having trouble doing both at the same time.

Old Pedant
05-14-2009, 10:51 PM
You can't just "wait a few ms" inside of a loop in JS coding.

You have to use setTimeout to then execute some code after a number of milliseconds. But when you make the call to setTimeout( ), it returns to the caller *immediately* without waiting.

Try to show some code and we'll see if we can find a different way to do what you are attempting.

And by the way, in JS code if you are in a tight loop, then no other code *can* set that variable. So in any case you have to restructure the code.

isjhe
05-14-2009, 11:37 PM
So setTimeout('doNothing()', 300); means the script will wait 300ms to execute doNothing(), while immediately continue with the rest of the loop, essentially ending the loop with 5 iterations of doNothing() executing in sequence?


Mmmm, that would put a kink in this, as well as fully explain some of the behavior I was noticing before.

In that case I'm going to have to restructure the whole function, 'cause that's not going to work for me at all.

*time passes*

Ok, I switched things around and got everything to work OK. I did some testing and found out that the resource I was waiting for never takes longer than 300ms, so I set my master function to run 1000ms later, and everything is good. It's not a big deal if this particular script is cobbled together in a hackish way, only 4 people will be using it.


For curiosities sake, is the core of my question possible (2 conditions to exit a loop)? I don't think I need functioning code in verbatim, but if anyone could point me in the right direction I think I can figure it out.

Thanks for the reply and tips Old Pedant.

Old Pedant
05-15-2009, 12:15 AM
So setTimeout('doNothing()', 300); means the script will wait 300ms to execute doNothing(), while immediately continue with the rest of the loop, essentially ending the loop with 5 iterations of doNothing() executing in sequence?

Exactly. 300 milliseconds after the loop ends (give or take).

You can always exit a loop at any time using the break keyword:

Silly example:


var iter = 0;
while ( iter++ < 10 )
{
var r = Math.floor( Math.random() * 1000 ) + 1;
if ( r > 950 ) break;
if ( prompt("Is " + r + " your number?" ) break;
}

adios
05-15-2009, 12:43 AM
This is really rough, and JS timers are tricky, but see if it helps.


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>untitled</title>
<style type="text/css">
body {font:12px monospace;margin:40px;}
</style>
<script type = "text/javascript">

function poller (polled, val, interval, attempts, action, msg)
{
var timer = setInterval(function()
{
if (eval(x) == val)
{
clearInterval(timer);
action('x=2');
}
else attempts--;
document.getElementById('readout').value = attempts; //demo, remove
if (attempts == 0)
{
clearInterval(timer);
alert(msg);
}
}, interval
);
}

function success(x)
{
alert(x);
}

var x = 0;

function pstart(box)
{
box.checked = false;
box.style.visibility = 'hidden';
document.getElementsByTagName('input')[1].checked = false;

poller(
'x', //variable checked
2, //good value
100, //interval ms.
100, //tries
success, //handler
'Error!' //error msg
);
}

</script>
</head>
<body>
&raquo;&raquo; var x=0<br /><br />
&raquo;&raquo; waiting for x to equal 2<br /><br />
&raquo;&raquo; onclick="x=1" <input name="variable" type="radio" onclick="x=1" /><br />
&raquo;&raquo; onclick="x=2" <input name="variable" type="radio" onclick="x=2" /><br />
&raquo;&raquo; onclick="x=3" <input name="variable" type="radio" onclick="x=3" /><br /><br />
&raquo;&raquo; remaining reads <input id="readout" type="text" style="border:none;" /><br /><br />
&raquo;&raquo; start <input type="checkbox" onclick="pstart(this)" /><br /><br />
<hr />
<script type = "text/javascript">
document.write(
'<pre>' ,
poller ,
'</pre>'
);
</script>
</body>
</html>


Click 'def' to set the desired value, or wait it out ...

Old Pedant
05-15-2009, 12:50 AM
Looks pretty good, Adios. I would have just created the function non-anonymously, and I'd probably have used setTimeout so I didn't need to bother with clearing the interval, but those are just details.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum