JS clearInterval or window.clearInterval?

让人想犯罪 __ 提交于 2019-12-23 15:29:05

问题


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.


回答1:


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.




回答2:


There is no real difference

This is basically the same as the following where global variable are a properties of the window object.

var myvar = "hello";

alert(myvar);

alert(window.myvar);

or where global functions are properties of the window object.

document.getElementById("myID");

window.document.getElementById("myID");



回答3:


window is the global context object. If you are not in a function that has had it's scope modified, everything you type is implicitly preceded by window..

var a = 0;
window.a = 0;

setTimeout(foo, 1000);
window.setTimeout(foo, 1000);

alert(this == window); //true


来源:https://stackoverflow.com/questions/8347248/js-clearinterval-or-window-clearinterval

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!