Simon Bauman
Posts: 0
Joined: Wed Oct 16, 2013 9:50 pm

Can't stop a setInterval call

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

Kateryna Grynko
Posts: 0
Joined: Thu Nov 15, 2012 9:13 am

Can't stop a setInterval call

Hi Simon,

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

Simon Bauman
Posts: 0
Joined: Wed Oct 16, 2013 9:50 pm

Can't stop a setInterval call

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

Kateryna Grynko
Posts: 0
Joined: Thu Nov 15, 2012 9:13 am

Can't stop a setInterval call

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.

Return to “Issues”