使用window.open();打开新标签页后延迟? [英] Delay after opening a new tab using window.open();?
问题描述
我正在寻找可在特定时间间隔后自动打开新标签页(windows)的javascript代码。
I am looking for javascript code which will open new tabs(windows) automatically after specific interval of time.
一旦执行第一个URL,setTimeout()/ setIntervals ()被忽略了吗?
请帮助!
Once first url is executed, setTimeout()/setIntervals()'s are ignored?? Please help!
<html>
<head>
<script type="text/javascript">
function open_win() {
setInterval(window.open("http://www.google.com"), 1000);
setInterval(window.open("http://www.yahoo.com"), 1000);
setInterval(window.open("http://www.bing.com"), 1000);
}
</script>
</head>
<body>
<form>
<input type=button value="Open Windows" onclick="open_win()">
</form>
</body>
谢谢
推荐答案
首先,您不想为此使用 setInterval
, setInterval
:
First of all, you don't want to use setInterval
for this, setInterval
:
重复调用一个函数或执行一段代码,每次调用该函数之间都有固定的时间延迟。
Calls a function or executes a code snippet repeatedly, with a fixed time delay between each call to that function.
您要使用 setTimeout
其中:
You want to use setTimeout
which:
在a之后调用函数或执行代码段指定的延迟。
Calls a function or executes a code snippet after a specified delay.
下一个问题是 setTimeout
想要一个函数作为其第一个参数,但 window.open
返回对wi的引用ndow对象。您想将那些 window.open
调用包装在函数内部:
The next problem is that setTimeout
wants a function as its first argument but window.open
returns a reference to a window object. You want to wrap those window.open
calls inside functions:
function open_win() {
setTimeout(function() { window.open("http://www.google.com") }, 1000);
setTimeout(function() { window.open("http://www.yahoo.com") }, 1000);
setTimeout(function() { window.open("http://www.bing.com") }, 1000);
}
您的版本将打开Google标签,因为窗口.open( http://www.google.com)
调用将在构建第一个 setInterval
调用的参数列表时执行。假定您在传递给它一个窗口引用时从 setInterval
中获取了异常或其他东西,所以甚至没有到达它们。
Your version would open the Google tab because the window.open("http://www.google.com")
call would be executed while building the argument list for the first setInterval
call. Presumably you're getting an exception or something from setInterval
when you pass it a window reference so the rest of them aren't even reached.
这篇关于使用window.open();打开新标签页后延迟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!