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:
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.
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.
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.
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.
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?
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:
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
Last edited by feyd on Fri Jan 13, 2006 7:32 pm, edited 1 time in total.