Start And Stop A Javascript Refresh
Solution 1:
Do this:
functiontimedRefresh(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.
Solution 2:
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;
functiontimedRefresh(timeoutPeriod){
resetId=setTimeout("location.reload(true);",timeoutPeriod);
}
Solution 3:
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
Solution 4:
The easiest way is to add the resetId
variable to the global scope:
var resetId = 0;
functiontimedRefresh(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
Post a Comment for "Start And Stop A Javascript Refresh"