使用 setTimeout 控制弹出 windows

Control over popup windows using setTimeout

我正在为我的女朋友制作一个网站作为节日礼物。我们都热爱艺术、代码和音乐,是的! 我正在尝试制作一个只有一个 button/link 的网站,点击它后,该网站开始播放音乐,随着音乐的播放,打开和关闭弹出窗口 windows 以及一些 gif 和其他内容。问题是,我很难在我的第一个弹出窗口 window 上应用延迟,它会自动关闭,但即使在阅读并尝试了本主题中其他问题的代码之后,我也无法延迟打开弹出窗口以正常工作。另外,我无法将弹出窗口放置在屏幕中的特定位置 :(( 这就是我所拥有的:

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title> pop ups </title>
<script>

var theURL = 'http://49.media.tumblr.com/tumblr_m3f88eRfVg1qcy0o0o1_500.gif';
var width  = 420;
var height = 315;

function popWindow() {
newWindow = window.open(theURL,'newWindow','toolbar=no,menubar=no,resizable=no,scrollbars=no,status=no,location=no,width='+width+',height='+height);


setTimeout(function() {
newWindow.close();
}, 3000)
} ;

</script>

</head>

<body>

    <p><a href="javascript:popWindow()"> temporalmente </a>      </p>

</body>

</html>

所以我在这里测试你的代码,它似乎工作正常。也许问题出在您使用的浏览器上。在 google chrome 中尝试您的代码,延迟应该有效。关于页面中的弹出位置,您想在 window.open 的第三个参数中添加带有数字值的顶部和左侧 属性。类似于:

...',height='+height + ', left=500, top=500')

要延迟打开 window,请使用另一个 setTimeout

function popWindow() {
    setTimeout(function() {
        var newWindow = window.open(theURL,'newWindow','toolbar=no,menubar=no,resizable=no,scrollbars=no,status=no,location=no,width='+width+',height='+height);

        setTimeout(function() {
            newWindow.close();
        }, 3000);
    }, 5000);
}