Javascript has setInterval and clearInterval functions for handling asynchronous function calls.
Is there a difference between clearInterval(handle)
and window.clearInterval(handle)
?
I've seen it being used both ways.
Definition and Usage The clearInterval() method clears a timer set with the setInterval() method.
In order to understand why setInterval is evil we need to keep in mind a fact that javascript is essentially single threaded, meaning it will not perform more than one operation at a time.
The clearInterval() function in javascript clears the interval which has been set by setInterval() function before that. setInterval() function takes two parameters. First a function to be executed and second after how much time (in ms). setInterval() executes the passed function for the given time interval.
I did make a fiddle to test it, and it turns out clearInterval stops all future execution, even those that have already be queued.
In a browser, all global functions are implicitly properties of the window
object. So clearInterval()
and window.clearInterval()
are the exact same thing.
There is no difference between them unless you define a local function called clearInterval()
, in which case window.clearInterval()
would reference the global one and clearInterval()
would reference the local one.
The same would be true for any global functions that you define yourself.
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