Start and Stop a javascript refresh
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开发者_C百科 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.
You're right, the problem is the scope of the variable resetId
. As you declare it in the function with the var
keyword, it is local to this function, and thus does not exist outside of it.
Simply declare resetId
outside of the function to make it global, and you'll be fine :
var resetId = 0;
function timedRefresh(timeoutPeriod){
resetId=setTimeout("location.reload(true);",timeoutPeriod);
}
I'd suggest making the content on the page, that needs to be refreshed, updated through ajax. Here is a link to a tutorial http://www.w3schools.com/Ajax/Default.Asp
The easiest way is to add the resetId
variable to the global scope:
var resetId = 0;
function timedRefresh(timeoutPeriod){
resetId=setTimeout("location.reload(true);",timeoutPeriod);
}
Alternatively, you might be able to define the iBox's hide function in the same scope, but I'm not too sure how iBox works.
More reading on scopes: http://javascript.about.com/library/bltut07.htm
精彩评论