user3073240
user3073240

Reputation: 573

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

I would like to know what the difference is between

window.setTimeout(myFancyFunciton, 1000); 

and

setTimeout(myFancyFunciton, 1000);

Both seem to do the exact same thing. When should you use one or the other?

Upvotes: 43

Views: 23350

Answers (5)

Oswald
Oswald

Reputation: 31647

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

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

The DOM specifies that the global object has a property named window, which is a reference back to the global object. That's why you can call window.setTimeout() and (by transitivity) window.window.setTimeout(), window.window.window.setTimeout(), and (you guessed it) window.window.window.window.window.window.window.window.window.setTimeout(). It's all the same method of the same object.

Upvotes: 46

dhilt
dhilt

Reputation: 20744

I faced an issue related to this topic. I tried to make some functionality of my SPA to be a part of server side rendering proccess. I used setTimeout to provide some deferred action on the UI. When it works on server side (NodeJS) it turns into deferred action on the server side with no relation to the client side. It's because of Browser setTimeout (say window.setTimeout) is not the same as NodeJS setTimeout. Apart from different runtime environments, which prohibits using a single setTimeout both for client side and server side rendering, the implementations of Browser and NodeJs setTimeout are different, they have different return value... Now I'm looking for some workaround.

Upvotes: 0

Luca Rainone
Luca Rainone

Reputation: 16458

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

The window object represents the window itself.

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

Why this? Think about a page with 2 or more frames. Every frame has own window. You can access to a variable inside a frame from another frame simply accessing to the window object of the target. This is valid for every variable or function declared as global... and it's valid too for native functions, like setTimeout.

So why sometimes we need to write explicity window.setTimeout?

Simply, if you are inside a scope and you use the same name of a native function, you can choose which function to use.

for example:

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();

Please note that the object window exists only in browser environment. The global object of Node.js is global where window is not defined.

Upvotes: 3

Madara's Ghost
Madara's Ghost

Reputation: 174957

Assuming we're talking about browser-based JavaScript: No difference. setTimeout() simply omits the window., which is implied. The effect they have is exactly the same.

It's a choice of coding style and preference.

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

Upvotes: 16

Alex Shilman
Alex Shilman

Reputation: 1547

It is exactly the same. Window is implicit if you don't specify it. Check out possible duplicate:

Is there a need to prepend setTimeout and setInterval with window object?

Upvotes: 0

Related Questions