What is the difference between window.setTimeout () and setTimeout ()?

I would like to know what is the difference between

window.setTimeout(myFancyFunciton, 1000); 

and

 setTimeout(myFancyFunciton, 1000); 

Both seem to be doing the same thing. When should you use one or the other?

+8
javascript settimeout
source share
5 answers

JavaScript runs in an environment that is defined by a global object. Methods of a global object can be called without explicitly accessing the object (i.e., without the notation obj.function() ).

When you run JavaScript inside the browser, a global object is provided by the Document Object Model (DOM). The global DOM object has a setTimeout() method. Therefore, you can call setTimeout() .

The DOM indicates that the global object has a property called window , which is a reference to the global object. So you can call window.setTimeout() and (for transitivity) window.window.setTimeout() , window.window.window.setTimeout() , and (you guessed it) window.window.window.window.window.window.window.window.window.setTimeout() . This is all the same method of the same object.

+21
source share

Assuming we are talking about a browser based on JavaScript: no difference. setTimeout() just omits the window. which is implied . The effect they have is exactly the same.

It is a choice of style and coding preference.

For JavaScript that does not run in the browser, the window object is not defined, so window.setTimeout() will fail. setTimeout() however, will work.

+4
source share

From https://developer.mozilla.org/en-US/docs/Web/API/Window

The window object is the window itself.

Thus, all the variables and functions that you call are enclosed inside the object window. However, you can omit the reference to the object each time you call a function or variable.

Why is this? Think of a page with 2 or more frames. Each frame has its own window . You can access a variable inside a frame from another frame simply by accessing the window object of the object. This is true for every variable or function declared as global ... and it is also valid for built-in functions such as setTimeout .

So why sometimes we need to write a window.setTimeout explication?

Simply, if you are inside the scope and use the same name for your own function, you can choose which function to use.

eg:

 function myF() { function setTimeout(callback,seconds) { // call the native setTimeout function return window.setTimeout(callback,seconds*1000); } // call your own setTimeout function (with seconds instead of milliseconds) setTimeout(function() {console.log("hi"); },3); } myF(); 

Note that the window object only exists in the browser environment. The global Node.js object is global , where window not defined.

+2
source share

This is exactly the same. A window is implicit unless you specify it. Check for a possible duplicate:

Is there a need for the setTimeout and setInterval prefix with the window?

0
source share

I am having a problem with this topic. I tried to make some functions of my SPA to be part of the server side rendering process. I used setTimeout to provide some pending actions in the user interface. When it runs on the server side (NodeJS), it switches to a pending action on the server side without regard to client side. This is due to the setTimeout browser (say window.setTimeout ) does not match the NodeJS setTimeout . In addition to different runtime environments that prohibit using the same setTimeout for rendering on the client side and on the server side, the implementations of Browser and NodeJs setTimeout different, they have a different return value ... Now I'm looking for a workaround.

0
source share

All Articles