Forum Moderators: open

Message Too Old, No Replies

[JS] Problems with clearing intervals

         

Veil

4:10 am on Jul 24, 2007 (gmt 0)

10+ Year Member



Hi there! Having some problems with clearing intervals, hope you can help me. Sorry for the long post, couldn't get it shorter :(

THE SITUATION
I have a <div> in which I load some stuff. Let's say page A and page B. Page A needs to reload every 10 seconds, page B doesn't need to reload. When I load page A into the div, I use setInterval to reload it. When I load page B, I use clearInterval to prevent page A from reloading, when page B is being shown.

THE PROBLEM
It works, until I load page A more then 1 time. For instance, I click on a link "show page A" 2 times, so twice it will do a setInterval. When I click on "show page B", it'll use a clearInterval, but for some reason page A keeps reloading. I guess the second interval hasn't been stopped? Although it's the same variable?

THE CODE
The div:

<div id="page"></div>

The scripts:


function getPage(div,page) {
//some ajax stuff loads certain things into the specified div
}


function reloadPage(div,vars) {
reload = setInterval("getPage('page','a')",10000);
}

The links:

<a href="#" onclick="getPage('page','a');reloadPage('page','a')">Load page A and keep reloading</a>

<a href="#" onclick="getPage('page','b');clearInterval(reload);">Load page B and stop the reload thingy</a>

HOW IT WORKS
So, when I click the page A link. It will load page A into the div, and binds the variable "reload" to a setInterval. This works, the page reloads.

Then, I click the page B link. It will load page B into the div, and clears the variable "reload", and thereby the interval. This works, the page doesn't reload anymore, the interval stopped.

Now, when I click page A link twice or more. I reloads fine. But then I click the page B link, and it doesn't stop (all) the interval(s). Although I loaded it multiple times, they are all put into the variable "reload". So why doesn't it stop when I clearInterval(reload)?

Thanks for your input!

Drag_Racer

11:11 am on Jul 24, 2007 (gmt 0)

10+ Year Member



when you load page A, clear out the setInterval before you set it.

Veil

11:27 am on Jul 24, 2007 (gmt 0)

10+ Year Member



Hm, now you mention it, that would make sense :D

It works, thanks you!