Scope of event handlers?

J

Jack

I have a script in which a function launched by a START button
continuously calculates and writes a value to a text box. The
calculation is done in a for loop. In the loop is a conditional that is
a global variable, a boolean. If the boolean is true, break ends the
loop (or is supposed to!). A STOP button has an onclick function that
sets the global variable to true.
What happens, though, is that the function for the STOP button is
not executed until the for loop reaches the maximum value set for i.
Anyone know how you can get one button to stop a process started by
another???
 
L

Lee

Jack said:
I have a script in which a function launched by a START button
continuously calculates and writes a value to a text box. The
calculation is done in a for loop. In the loop is a conditional that is
a global variable, a boolean. If the boolean is true, break ends the
loop (or is supposed to!). A STOP button has an onclick function that
sets the global variable to true.
What happens, though, is that the function for the STOP button is
not executed until the for loop reaches the maximum value set for i.
Anyone know how you can get one button to stop a process started by
another???

The general method you've described should work.
Seeing your code would help us to spot the problem.
 
R

RobG

Jack wrote:
[...]
Anyone know how you can get one button to stop a process started by
another???

What you are attempting is probably something like:

<input type="button" value="start" onclick="
keepGoing = true;
for (var i=0; i<100000 && keepGoing; i++) {
this.form.counter.value = i;
}
">
<input type="button" value="stop" onclick="
keepGoing = false;
">

When you click the "start" button, the counter starts. But the
script blocks further input until it is finished, so clicking the
"stop" button does nothing.

The trick is to start the script using setInterval - which will
run the script at a regular intervals - or setTimeout, which will
run the script after a specified delay. During the pauses, other
input will be accepted, such as a click on the "stop" button to
change keepGoing to false.

The effect you are looking for can be created using setTimeout as
follows:

<script type="text/javascript">
function startCount() {
if (y.value < 1000 && keepGoing) {
y.value -= -1;
setTimeout("startCount(y)",10);
}
}
</script>
<form action="">
<input type="text" name="counter" value="0">
<input type="button" value="start" onclick="
keepGoing = true; // global boolean
y = this.form.counter; // output cell is global too
y.value = 0; // reset value to zero
startCount(y);
">
<input type="button" value="stop" onclick="
keepGoing = false;
">
</form>


Though be warned, if the script that is run by setTimeout
consumes the entire delay, it will effectively block all input
anyway and you likely will not be able to enter further input.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,434
Messages
2,571,690
Members
48,796
Latest member
Greg L.

Latest Threads

Top