By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,516 Members | 1,153 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,516 IT Pros & Developers. It's quick & easy.

Scope of event handlers?

P: n/a
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???

Jul 23 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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.

Jul 23 '05 #2

P: n/a
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.

--
Rob
Jul 23 '05 #3

P: n/a
Here's the code:

<html>
<head>
<title>test</title>

<script type="text/javascript">
var gregStop = false;
var gregTerms = 1;
var gregCalc = 0;
var gregPI = 0;

function grgStart(S){
for (i = 1;i<1000;i++) {
if (gregStop) {break};
var thisdenom = (2*gregTerms)-1;
var newterm = 1/thisdenom;
if ( (gregTerms%2)==0) {
gregCalc = gregCalc - newterm;
}
else {gregCalc = gregCalc + newterm};
gregPI = 4*gregCalc;
document.gregory.gPI.value = gregPI;
document.gregory.gterms.value= gregTerms;
document.gregory.gterm.value = thisdenom;
gregTerms++;
}
}

function grgStop(){
gregStop = true;
}

function grgClear(){
gregStop = false;
gregTerms = 1;
gregCalc = 0;
gregPI = 0;
document.gregory.gPI.value = 0;
document.gregory.gterms.value= 0;
document.gregory.gterm.value = "";

}
</script>
</head>
<body>
<form name="gregory" action="">
<p>Calculated value:&nbsp;<input type="text" size="40" name="gPI"
value="0" onfocus="blur()" />
after calculating&nbsp;<input type="text" size="15" name="gterms"
value="0" onfocus="blur()" />terms. <br />
The current term is 1 over <input type="text" size="15" name="gterm"
value="0" onfocus="blur()" /><br />
<input type="button" name="gstart" value="START" onclick="grgStart()" />
<input type="button" name="gstop" value="STOP" onclick="grgStop()" />
<input type="button" name="gclr" value="CLEAR" onclick="grgClear()"
/></p>
</form>
</body>
</html>

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 23 '05 #4

P: n/a
Rob--
The script did consume the entire delay, but you set me on the right
track, Combining setInterval and setTimeout can create an open period
in which the click on the stop button is captured. Now have a version
that works. Thanks.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.