PDA

View Full Version : Processing user input inside a loop


Vladdy
11-18-2002, 01:25 PM
Hi all,

Would like to hear your opinions on this one:

say I have a variable that is updated using some onclick event and I want to have a loop where I wait for this variable to be set to some value. What do I do inside the loop that will allow to process user input and not hang the browser?


variable = 0

while (variable!=5)
{
// do something here that will allow another function
// envoked by some event to change the variable
}

Consider it a theoretical problem (do not ask me what I'm trying to do, 'cause I'm aware or other approaches)

Regards

Roelf
11-18-2002, 01:29 PM
i would use setinterval to launch a function at a fixed interval rate. in the function test for your variable to change to the desired value

Vladdy
11-18-2002, 01:50 PM
The problem is to stay within the loop!!!

Roelf
11-18-2002, 01:52 PM
are you trying to create a wait() statement or something?

RadarBob
11-18-2002, 09:01 PM
Somehow I just don't see it happening. This is not Ada, with (multi) tasking and "rendezvous."

glenngv
11-19-2002, 04:55 AM
do away with while loop (or any loop for that matter), create a function that can accomplish the same thing and then use setInterval or setTimeout to call it:


<html>
<head>
<script>
var myvar = 0;
var t;
function myloop(){
if (myvar==-1) alert('myvar changed!');
else window.status=myvar;
myvar++;
}
</script>
</head>
<body onload="t=setInterval('myloop()',10)">
<form>
<input type='button' value="Change" onclick="myvar = -1">
</form>
</body>
</html>