I am working on an application, where I have one page whose partial content need updating in a interval. I am thinking using JavaScript setInterval to do this task, but I have not used this method before, so how can I start setinterval when that specific page is loaded and stop setinterval before the page unloaded.
by the way, if you know of any better way to do this kind of task, please be kind to share with me. thanks for any help.
Given that you have a function
To run it every
nmilliseconds, useYou won’t need to stop the interval yourself when the page is ‘unloaded’ or traversed away from; this is done automatically. If, however, you wish to stop it at any other time, then save the return value of
setIntervalwhen you use it, like this:and use this to stop the interval timer: