I have a page that needs to be refreshed every 60 seconds. On this page I am using iBox to popup various items. My problem is that a meta refresh kills the popup which is not wanted. Please keep in mind I have zero to little experience with javascript so my solution may be fundamentally wrong.
The solution I came up with is to use javascript to do the refresh. When the page loads I will start the timer, when an ibox pops up I will clear the timer, when the ibox closes I will start the timer again.
I am setting this up by using a simple function.
function timedRefresh(timeoutPeriod){
var resetId = 0;
resetId=setTimeout("location.reload(true);",timeoutPeriod);
}
Then I call the function <body onload="timedRefresh(60000)">.
My problem is stemming from when I try to call clearTimeout(resetID). I am trying to call the method from the ibox script's hide function, but it doesnt actually clear out the timer. I think it may be a scope issue and I may need to do some sort of Object.clearTimeout(Object.resetID) but that is just a guess.
Do this:
function timedRefresh(timeoutPeriod){
window.resetId = 0; // make it clear it's global by prefixing "window."
window.resetId=setTimeout("location.reload(true);",timeoutPeriod);
}
Then from the relevant ibox function use window.resetId.
Seeing your comment I'll add something.
"window." will work when scripting within a browser, should you use JS somewhere else it probably won't work.
As long as you're on a webpage, however, window is the global object and the "window." prefix is IMO a good way to make it clear that certain variables are global; if you use it consistently, all the variables that don't have "window." in front of them are always local.
You should know, however, that it would also work if you simply used resetId without any prefix and without var because any variable that isn't declared with var is automatically scoped to window.
This short-ish guide will teach you most of what you need to know about variable visibility in Javascript, execution contexts and closures. It will send you on your way to become a deadly Javascript ninja.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With