Forum Moderators: open
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!