I have a Div that uses jQuery to load a file/contents with a javascript function..
function DoWork() {
// Do Stuff
}
Let’s say the user can reload the Div and pull the same file/contents with the same js function DoWork(). The problem is, when the file is reloaded, the previous loaded function DoWork() is still running. How can I kill the previous fired DoWork() and restart it?
Javascript is single-threaded, which means only one thing can be executing at a given moment. If
DoWorkis already “running” it’s either a) blocking all other JS code, and you have no choice but to let it finish since you have no way to execute any interruption code until it finishes on its own, or b)DoWorkis scheduled to fire off on an interval viasetTimeout()orsetInterval().If it’s the latter case,
setTimeout()andsetInterval()return an ID. Store that ID somewhere and callclearTimeout(doWork_timeout_id)orclearInterval(doWork_interval_id)according to how you started it.