473,387 Members | 1,520 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Scope of event handlers?

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
4 1365
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Marc Tanner | last post by:
Hello, I am currently working on a eventhandling system or something similar, and have the problem of loosing scope. I have read many interesting posts on this group and the faq article about...
8
by: Dennis C. Drumm | last post by:
I have a class derived from a SortedList called SystemList that contains a list of objects indexed with a string value. The definition of the objects contained in the SortedList have a boolean...
19
by: Dave | last post by:
I'm building a research application that needs to be a super speed demon. I decided that one way to do this is to use goto loops instead of while() loops when I need them. that way, instead of...
13
by: Charles Law | last post by:
Mr "yEaH rIgHt" posted the following link about a week ago in answer to my question about removing event handlers. > http://www.vbinfozine.com/t_bindevt.shtml Following on from that post, the...
5
by: Amit | last post by:
Hello! Is it possible to find how many event handlers an event has at runtime? How about finding whether or not an event has event handlers? In C# you can compare the event with null to check if...
6
by: Kevin Attard | last post by:
I am using a GridView inside a UserControl which has a template column for deleting the rows. Before databinding the gridview i am attaching the RowCommand and RowDataBound event. I am using the...
16
by: Hamed | last post by:
Hello I am developing a utility to be reused in other programs. It I have an object of type Control (a TextBox, ComboBox, etc.) that other programmers use it in applications. they may set some...
14
by: Hamed | last post by:
Hello It seems that I should implement ICloneable to implement my own clone object. the critical point for me is to make a control object based on another control object that all of its event...
2
by: Gabe Moothart | last post by:
Hello, In one of my asp.net applications, I create a series of checkboxes, set their properties, and give them an "onChecked" event handler on the fly using an anonymous method. The code looks...
6
by: Tony Johansson | last post by:
Hello! I know it's possible to have several event handler for a single event but I can't see any point using several event handler for this. I'm probably wrong so can anybody tell me what...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.