/    Sign up×
Community /Pin to ProfileBookmark

Breaking out of setInterval()

Purpose: any number of fields can sum into a target field.
Example: first_floor_area+ second_floor_area = total_living_area
Using the following event handlers:

[code]onfocus=”startAdding(‘total_living_area’);” onblur=”stopAdding();”[/code]

Automatically generated javascript looks like this:

[code]
var sInterval; // for calculating sum of data in text fields
function startAdding(sumField){
var subFunc = ‘sum_’ + sumField + ‘()’;
sInterval = setInterval(subFunc,1); // EVALUATE ‘sum_’ + ‘sumField’ at interval of 1 millisecond
}
function stopAdding(){
clearInterval(sInterval);
}
// Sum calculation functions for form number 0
function sum_total_living_area(){
document.form0.total_living_area.removeAttribute(‘readOnly’);
var part1 = GetValue(‘basement’);
var part2 = GetValue(‘first_floor’);
var part3 = GetValue(‘second_floor’);
var part4 = GetValue(‘third_floor’);
document.form0.total_living_area.value = (part1 * 1.0) + (part2 * 1.0) + (part3 * 1.0) + (part4 * 1.0);
document.form0.total_living_area.setAttribute(‘readOnly’,’true’);
}
[/code]

A simple rendition of GetValue looks like this:

[code]
function GetValue(ID){
var widget = document.getElementById(ID);
return widget.value;
}
[/code]

This method works well. I would like to add the following enhancement:
If the user has added a non-integer value, I’d like to do the following:
1)Inform the user by an alert() function that the input is invalid.
2)Put focus on the field with the invalid data.
3)Break of out the setInterval loop.

I’ve made several attempts to implement this error-checking and the result has been an unterminated series of alert boxes, necessitating killing the browser.

So what I would like to do as ask some guidance and opinions.
First of all, would I pass sInterval (my clone of setInterval) to GetValue()?
Then, following the extraction of data for the widget, should it be NaN, then
I would provide the alert() call, then a call to widget.focus().

At what point should I call clearInterval()?

Now, I’m attempting to do all this in the onfocus evenhandler. Maybe I’m
barking up the wrong tree and onblur should be handling this.

? Inquiring minds want to know!
Hope I’m being clear here.
Feel free to point me to relevant discussions or examples.
(I love examples)
thanks
tim

to post a comment
JavaScript

0Be the first to comment 😎

×

Success!

Help @tim042849 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...