为什么这个setTimeout不起作用 [英] why is this setTimeout not working

查看:128
本文介绍了为什么这个setTimeout不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚进入Java。我正在编写一个简单的脚本来打开一个窗口,然后在短暂的延迟后关闭它。我尝试了以下各种内涵,但无济于事。该函数有效(因为它打开,然后关闭窗口),但延迟不会发生。

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.

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

谢谢

推荐答案

你想:

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

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

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.

示例:

<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>

这篇关于为什么这个setTimeout不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆