User Interrupts in JavaScript Loops?

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
llanitedave
Forum Commoner
Posts: 78
Joined: Thu Jan 15, 2004 11:24 am
Location: Las Vegas, NV.

User Interrupts in JavaScript Loops?

Post 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.
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post 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.
User avatar
llanitedave
Forum Commoner
Posts: 78
Joined: Thu Jan 15, 2004 11:24 am
Location: Las Vegas, NV.

Post 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.
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

Well it depends on what your doing, if your using buttons then onClick=runSomeFunction() inside the button tag.
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

psuedocode
var x = false

while(!x){
whatev
}


meanwhile...

onAction(){
x=true;
}
User avatar
llanitedave
Forum Commoner
Posts: 78
Joined: Thu Jan 15, 2004 11:24 am
Location: Las Vegas, NV.

Post 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.
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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
Last edited by feyd on Fri Jan 13, 2006 7:32 pm, edited 1 time in total.
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

Ahh yea setInterval, that was it!
User avatar
llanitedave
Forum Commoner
Posts: 78
Joined: Thu Jan 15, 2004 11:24 am
Location: Las Vegas, NV.

Post 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!
User avatar
llanitedave
Forum Commoner
Posts: 78
Joined: Thu Jan 15, 2004 11:24 am
Location: Las Vegas, NV.

Post 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...
Post Reply