Javascript 为什么这个 setTimeout 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5116223/
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-08-23 15:43:49  来源:igfitidea点击:

why is this setTimeout not working

javascript

提问by Gilles

I'm just getting into Java. I'm working on a simple script to open a window, then close it after a short delay. I've tried various connotations of the following with no avail. The function works (in that it opens, then closes the window) but the delay fails to happen.

我刚开始接触 Java。我正在编写一个简单的脚本来打开一个窗口,然后在短暂的延迟后关闭它。我尝试了以下各种含义,但无济于事。该功能有效(因为它打开,然后关闭窗口)但延迟没有发生。

function manualWindow(){
testWindow = window.open("popup.php","interaction","resizable=0,width=800,height=600,status=0");
setTimeout(testWindow.close(),5000);
}

thank you

谢谢你

回答by Richard Dalton

You want:

你要:

setTimeout(function() { testWindow.close(); },5000);

You current code is executing that function as soon as it is hit and then trying to run it's return value after the delay. By wrapping it up in the function it will be run correctly after 5 seconds.

您当前的代码在它被击中后立即执行该函数,然后在延迟后尝试运行它的返回值。通过将其包装在函数中,它将在 5 秒后正确运行。

Example:

例子:

<html>
<head></head>
<body>
<script type="text/javascript">
    function manualWindow(){
       testWindow = window.open("http://www.google.co.uk","interaction","resizable=0,width=800,height=600,status=0");
       setTimeout(function() { testWindow.close() },5000);
    }

    manualWindow();
</script>
</body>
</html>

回答by vargas

Firstly, you're defining your code within an anonymous function. This construct:

首先,您在匿名函数中定义您的代码。这个构造:

(function() {
  ...
)();

does two things. It defines an anonymous function and calls it. There are scope reasons to do this but I'm not sure it's what you actually want.

做两件事。它定义了一个匿名函数并调用它。这样做有一定的范围原因,但我不确定这是您真正想要的。

You're passing in a code block to setTimeout(). The problem is that update()is not within scope when executed like that. It however if you pass in a function pointer instead so this works:

您正在将代码块传递给setTimeout(). 问题是这样update()执行时不在范围内。但是,如果您传入一个函数指针,则此方法有效:

(function() {
  $(document).ready(function() {
    update();
  });
});

because the function pointer update is within scope of that block.

因为函数指针更新在该块的范围内。

But like I said, there is no need for the anonymous function so you can rewrite it like this:

但是就像我说的,不需要匿名函数,所以你可以像这样重写它:

回答by Richard H

You are not using setTimeout correctly. Try this:

您没有正确使用 setTimeout。尝试这个:

function manualWindow(){
   testWindow = window.open("popup.php","interaction","resizable=0,width=800,height=600,status=0");
   setTimeout(function() { testWindow.close(); },5000);
}

回答by Virtual Innovators

I think your code is missing the qutoes in the first parameter testWindow.close().This can be corrected in the following way:

我认为您的代码缺少第一个参数 testWindow.close() 中的 qutoes。这可以通过以下方式更正:

function manualWindow() {
    testWindow = window.open("http://www.google.com", "interaction", "resizable=0,width=800,height=600,status=0");
    setTimeout('testWindow.close()',5000);
}

manualWindow();

I found this link may help you a bit https://developer.mozilla.org/en/DOM/window.setTimeout

我发现此链接可能对您有所帮助https://developer.mozilla.org/en/DOM/window.setTimeout