Page 1 of 1

Can't stop a setInterval call

Posted: Wed Nov 20, 2013 5:46 pm
by Simon Bauman

I've got an onload setInterval countdown setup that I can't seem to stop from a button onclick event. ...getting a "timerId not defined" error on the onclick. Any ideas on a fix?

onload :

var timerId = setInterval(
function(){
.....etc

onclick :

clearInterval(timerId);

....also tried window.clearInterval(timerId);

Thanks


Can't stop a setInterval call

Posted: Wed Nov 20, 2013 6:38 pm
by Kateryna Grynko

Hi Simon,

You should pass 'timer' in clearInterval():preclearInterval(timer);/pre


Can't stop a setInterval call

Posted: Wed Nov 20, 2013 6:40 pm
by Simon Bauman

That doesn't work. Why would I pass "timer" when the object is set as "timerId"?

...researching this problem, I'm thinking it's a scope issue. The onclick event is trying to access the timerId object which was defined in onload (and is not global). Any ideas on how to access objects in the onload event(s)?

Thanks


Can't stop a setInterval call

Posted: Wed Nov 20, 2013 7:25 pm
by Kateryna Grynko

Simon,

[quote:]Why would I pass "timer" when the object is set as "timerId"?[/quote]Probably because you have just corrected your question :)

Make the timer variable global - declare it in custom JS code.