Page 1 of 1
User Interrupts in JavaScript Loops?
Posted: Fri Apr 02, 2004 9:18 am
by llanitedave
I haven't seen anything in the books that will help me do this:
I want to have a loop running that, without stopping, can test for user input that will stop it or change its behavior. It should go something like this:
Code: Select all
while (keepgoing(button_status)) {
move_image(image);
check_buttons();
}
All I can get is an endless loop.
Is there some way a user input can be detected during the running of a script?
All I can seem to find out how to do is trigger scripts. I'd like to know how to trap user input inside a running script without stopping to wait or ask for it.
Posted: Fri Apr 02, 2004 9:28 am
by kettle_drum
Only if the user does something like moves the mouse over something, selects a text box, goes out of a text box etc. But you dont need a loop to detect that, as you can just call the code you want run when the event occurs and javascript will do it.
Posted: Fri Apr 02, 2004 10:22 am
by llanitedave
kettle_drum wrote:Only if the user does something like moves the mouse over something, selects a text box, goes out of a text box etc. But you dont need a loop to detect that, as you can just call the code you want run when the event occurs and javascript will do it.
I'm thinking more of a start/stop button situation: Clicking one button starts the script running in a loop. The loop keeps running until the user either stops it by clicking the same or another button, or changes its behavior by maybe a third button.
Either way, you need to be able to interrupt the loop from a user event. And I can't seem to figure out how to do that.
Posted: Fri Apr 02, 2004 10:25 am
by kettle_drum
Well it depends on what your doing, if your using buttons then onClick=runSomeFunction() inside the button tag.
Posted: Fri Apr 02, 2004 10:35 am
by magicrobotmonkey
psuedocode
var x = false
while(!x){
whatev
}
meanwhile...
onAction(){
x=true;
}
Posted: Fri Apr 02, 2004 10:38 am
by llanitedave
Well I've tried that, of course, to set a global variable -- and then I check for the status of that variable at the end of each loop. But it doesn't seem to be getting through.
In fact, with the loop running I can't even get any UI elements to respond at all -- the loop takes up all of the processor time.
Is there a way to pause that allows me to check for user input, or at least allows the user to take an action? I tried setTimeout(), but that doesn't seem to have any effect.
Posted: Fri Apr 02, 2004 10:44 am
by magicrobotmonkey
Hmm... I remember making a stopwatch thing once for some programming class, but I'm not entirely sure how I did it... I think I must have used some sort of "wait" fxn or something. Because it showed the number constantly increasing until the user hit stop so it was something more than a simple subtract start time from end time deal. Gah I can't quite remember how I did it. What are you trying to do, anyways?
Posted: Fri Apr 02, 2004 12:09 pm
by feyd
creating a state machine like magicrobotmonkey suggested is a good start. Make the loop run on setInterval (storing the return value somewhere globally so you can kill it.) here's some example code:
Code: Select all
var fsm = 0;
var timer = null;
function machine()
{
switch(fsm)
{
case 0: // initial run, setup the interval
timer = setInterval("machine()",40);
fsm = 1;
break;
case 1:
window.status="state 1";
break;
case 2:
window.status="state 2";
break;
case 3:
window.status="state 3";
break;
}
}
----
<input type=button onclick="fsm = 1; return false" value="state 1">
<input type=button onclick="fsm = 2; return false" value="state 2">
<input type=button onclick="fsm = 3; return false" value="state 3">
you could make it lock-step as well. meaning that, while you are in the processing function, set another global variable to an altered state. Check against that when someone clicks to buttons, and don't allow changes while the locking variable is altered.
fixed old-new php parser errors
Posted: Fri Apr 02, 2004 12:18 pm
by magicrobotmonkey
Ahh yea setInterval, that was it!
Posted: Fri Apr 02, 2004 12:53 pm
by llanitedave
setInterval may be exactly what I was looking for -- and it IS in the book! Like so many other things, I just missed it.
I'll give it a shot -- and THANKS!
Posted: Tue Apr 06, 2004 4:00 pm
by llanitedave
This problem was not at all what I thought it was when I first posted it. I was misusing a global variable, that's all.
Note to newbies: Don't pass global variables as argments to functions!
It really confuses javascript!
Hard lesson learned...