简体   繁体   English

在新标签页和超时中打开网页

[英]Opening webpage in new tab and timeouts

I am wondering, what is happening to the setTimeout's, setInterval's and other functions that trigger when the DOM is loaded, when I open the webpage in other tab or window. 我想知道,当我在其他选项卡或窗口中打开网页时,setTimeout,setInterval和其他在加载DOM时触发的函数正在发生什么。 Do they trigger once I actually see the webpage, or they trigger once the DOM is loaded, even in the background? 它们是在我实际看到网页后触发,还是在DOM加载后即在后台触发? Is this different on different browsers? 这在不同的浏览器上是否不同?

At least in Chrome and Opera the callback is executed, even if the page is loaded in the background. 至少在Chrome和Opera中,即使页面是在后台加载的,回调执行。

Consider the following HTML 考虑以下HTML

<a href="javascript:openWindow()">Click me!</a>

and the respective JavaScript 以及相应的JavaScript

function openWindow()
{
    var article = open('');
    focus();

    article.setTimeout(function(){ article.alert("Hello"); }, 3000);
}

(See this fiddle ). (请参阅此小提琴 )。

If you click on the link with the Ctrl-Key pressed, Chrome opens a new background tab. 如果您在按住Ctrl键的同时单击链接,Chrome会打开一个新的背景标签。 Still the alert is shown after 3 seconds. 3秒后仍显示alert The same beaviour can be observed in Opera. 在Opera中可以观察到相同的行为。 I've tried Edge, too, but it won't open the page in a background tab, hence I cannot tell, if the JS would still be executed. 我也尝试过Edge,但是它不会在后台选项卡中打开页面,因此我无法确定JS是否仍将执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM