Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JS clearInterval or window.clearInterval?

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.

like image 685
TheOne Avatar asked Dec 01 '11 19:12

TheOne


People also ask

What is window clearInterval?

Definition and Usage The clearInterval() method clears a timer set with the setInterval() method.

Is it good to use setInterval in JavaScript?

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.

What does clearInterval do in JavaScript?

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.

Does clearInterval stop execution?

I did make a fiddle to test it, and it turns out clearInterval stops all future execution, even those that have already be queued.


1 Answers

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.

like image 65
jfriend00 Avatar answered Sep 28 '22 09:09

jfriend00