为什么 window.open() 在循环中通过 setTimeout() 调用时会被静默忽略?
--- 编辑.
我不明白;昨天它似乎被忽略了;现在它似乎正在工作.即使在循环中并通过 setTimeout()
调用.目前,我似乎很难复制昨天的行为......这是怎么回事?
I don't get it; yesterday it seemed to be ignored; now it seems to be working. Even within loop and called via setTimeout()
. Currently I seem to be having difficulties replicating yesterday's behaviour... What is going on?
--- 第二次编辑.
第一个想法是如何修复"问题的复制:奇怪的是,这是否有效似乎取决于当前的 URL!例如.作品来自SE 站点,但不是来自 http://www.asdf.com/.怎么样?
First idea how to "fix" the replication of the issue: weirdly, whether or not this works seems to be dependent on the current URL! E.g. works from SE-sites, but not from, say, http://www.asdf.com/. How so?
而 setTimeout()
在这里工作:
Whereas setTimeout()
works here:
setTimeout(function(){ alert("Hello"); }, 3000);
和 window.open()
在这里工作:
and window.open()
works here:
window.open("https://www.bbc.com","_self");
甚至两者的结合在这里工作:
and even the combination of the two works here:
setTimeout(function(){ window.open("https://www.bbc.com","_self") }, 3000);
出乎意料地,两者的结合在一个循环中被 - 默默地 - 忽略了:
suddently and unexpectedly the combination of the two is - silently - ignored in a loop:
i=0;
while(i < 100)
{
setTimeout(function(){ window.open("https://www.bbc.com","_self") }, 3000);
i++
}
为什么?
tldr;
这个问题似乎已经出现了近一百万次,但还没有(据我所知/搜索)采用简洁的问答格式;例如
This question seems to have come up close to a million times already but not yet (as far as I could tell / search) with a succinct Q&A format; e.g.
Settimeout() javascript 函数被忽略
从第一原理"模拟 JavaScript 的 setTimeout() 方法
推荐答案
弹出大多数流行浏览器中的阻止程序仅允许打开一个新窗口,如果它是由于直接用户操作(例如单击)运行的代码而打开的.因为 setTimeout()
发生在未来某个时间,不被认为是用户操作的直接结果,所以尝试从 setTimeout()
打开窗口可能会被弹出窗口拦截器.
Popup blockers in most popular browsers will only allow a new window to be opened if it is opened as a result of code running from a direct user action such as a click. Because a setTimeout()
happens some time in the future, is not considered the direct result of a user action so attempts to open windows from setTimeout()
are likely blocked by the popup blocker.
本质上,尝试从 window.open
从内部 setTimeout()
触发 浏览器 为认为这是一个值得(静默)阻止的弹出窗口.-- 相反,如果 window.open
是自己触发的,浏览器会将其视为就像用户点击",即不作为垃圾邮件被拦截.
In essence, trying to fire window.open
from within setTimeout()
leaves the browser to "think" it's a popup which deserves (silent) blocking. -- If, in contrast, window.open
is fired on it's own, the browser treats seems to treat it like a "user click", that is, not as spam to be blocked.
相关文章