javascript window.setTimeout() 和 setTimeout() 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20420429/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 18:26:48  来源:igfitidea点击:

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

javascriptsettimeout

提问by user3073240

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?

两者似乎都在做完全相同的事情。什么时候应该使用其中一种?

回答by Oswald

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

JavaScript 在由全局对象定义的环境中运行。可以在不显式引用对象的情况下调用全局对象的方法(即没有obj.function()符号)。

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

当您在浏览器中运行 JavaScript 时,全局对象由文档对象模型 (DOM) 提供。DOM 的全局对象有一个方法setTimeout()。这就是为什么您可以调用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.

DOM 指定全局对象有一个名为 的属性window,它是对全局对象的引用。这就是为什么你可以调用window.setTimeout()和(通过传递)window.window.setTimeout()window.window.window.setTimeout()和(你猜对了)window.window.window.window.window.window.window.window.window.setTimeout()。这都是同一个对象的相同方法。

回答by Madara's Ghost

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.

假设我们谈论的是基于浏览器的 JavaScript:没有区别。setTimeout()只是省略了window.,这是隐含的。它们的效果完全一样。

It's a choice of coding style and preference.

这是编码风格和偏好的选择。

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

对于不在浏览器中运行的 JavaScript,window对象未定义,因此window.setTimeout()会失败。setTimeout()但是,会起作用。

回答by Luca Rainone

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

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

The window object represents the window itself.

window 对象代表窗口本身。

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 windowobject of the target. This is valid for every variable or function declared as global... and it's valid too for native functions, like setTimeout.

为什么这个?考虑一个包含 2 个或更多框架的页面。每个框架都有自己的window. 您可以从另一个框架访问一个框架内的变量,只需访问window目标的对象。这对声明为全局的每个变量或函数都有效......并且对本机函数也有效,例如setTimeout.

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

那么为什么有时我们需要明确地写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 windowexists only in browser environment. The global object of Node.jsis globalwhere windowis not defined.

请注意,该对象window仅存在于浏览器环境中。的全局对象Node.jsglobalwherewindow未定义。

回答by Alex Shilman

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

这是完全一样的。如果您不指定,则 Window 是隐式的。查看可能的重复项:

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

是否需要在窗口对象前加上 setTimeout 和 setInterval?

回答by dhilt

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 setTimeoutto 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 setTimeoutboth for client side and server side rendering, the implementations of Browserand NodeJssetTimeoutare different, they have different return value... Now I'm looking for some workaround.

我遇到了与此主题相关的问题。我试图让我的 SPA 的一些功能成为服务器端渲染过程的一部分。我曾经setTimeout在 UI 上提供一些延迟操作。当它在服务器端(NodeJS)上工作时,它变成了服务器端的延迟操作,与客户端无关。这是因为 Browser setTimeout(比如说window.setTimeout)与 NodeJS 不同setTimeout。除了不同的运行时环境,禁止setTimeout在客户端和服务器端渲染时使用一个单一的,浏览器NodeJs的实现setTimeout是不同的,它们有不同的返回值......现在我正在寻找一些解决方法。