【问题标题】:Javascript to open URLs at after a delay延迟后打开 URL 的 Javascript
【发布时间】:2018-01-14 06:30:21
【问题描述】:

我根据自己的喜好使用this script,一切正常,但我想要一个更可靠的脚本。这是我的代码:

<script type="text/javascript">
function open_win() {
    setTimeout(function() { window.open("http://example.com/t101.php") }, 1000);
    setTimeout(function() { window.open("http://example.com/t102.php")  }, 1000);
    setTimeout(function() { window.open("http://example.com/t103.php")   }, 1000);
    setTimeout(function() { window.open("http://example.com/t104.php")   }, 1000);

    setTimeout(function() { window.open("http://example.com/t105.php") }, 120000);
    setTimeout(function() { window.open("http://example.com/t106.php")  }, 120000);
    setTimeout(function() { window.open("http://example.com/t107.php")   }, 120000);
    setTimeout(function() { window.open("http://example.com/t108.php")   }, 120000);

    setTimeout(function() { window.open("http://example.com/t109.php") }, 240000);
    setTimeout(function() { window.open("http://example.com/t110.php")  }, 240000);
    setTimeout(function() { window.open("http://example.com/t111.php")   }, 240000);
    setTimeout(function() { window.open("http://example.com/t112.php")   }, 240000);
    
    }
</script>
<input type=button value="Start Scan" onclick="open_win()">

我以 4 个一组的形式打开 URL,每个组用时不到 2 分钟,每个组执行的确切时间不具体/不知道,但不到 2 分钟。

我不想在特定时间打开每个组,而是想在第一组关闭时打开每个组,而不是等待 2 分钟。以上所有链接都使用window.close();在执行时自动关闭。

还有没有机会打开100个这样的链接而不使用这个功能?所有链接都以这样的编号结尾。

我是 JavaScript 的初学者。如有不妥请见谅。

【问题讨论】:

  • 我投票决定将此问题作为离题结束,因为 SO 是来帮助您编写代码的,但 SO 不是来为您编写代码的。
  • 所以你想一次打开 100 个链接?如果名称相同,则获取索引的循环就足够了
  • 好吧,如果你想检测从另一个窗口返回,那么恐怕你需要一个特定的返回 url。
  • 为此,你有什么尝试?

标签: javascript


【解决方案1】:

将您的 window.open 调用存储到一个变量中,例如:

var a = window.open(/*url...*/);

一旦该窗口关闭,它的closed 属性将为真。只需设置一个 setInterval() 每隔一秒左右检查一次以检查它是否已关闭,然后打开一个新窗口。这个question 有你的答案。像下面这样的东西可以工作:

var a = window.open("urla");
setInterval(function(){if (a.closed) {window.open("urlb");}}, 1000);

如果你想打开 100 个链接,你可以创建一个单独打开每个链接的 for 循环:

for (var a = 100; a < 200; a++) { window.open("http://example.com/t" + a + ".php"); }

^ 将打开从 100 到 199 的链接。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多