Why is window.open() silently ignored iff called from within a loop and via setTimeout()?(为什么 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.
这篇关于为什么 window.open() 在循环中通过 setTimeout() 调用时会被静默忽略?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么 window.open() 在循环中通过 setTimeout() 调用时会被静默忽略?
基础教程推荐
- 用于 Twitter 小部件宽度的 HTML/CSS 2022-01-01
- 我可以在浏览器中与Babel一起使用ES模块,而不捆绑我的代码吗? 2022-01-01
- Chart.js 在线性图表上拖动点 2022-01-01
- 如何使用TypeScrip将固定承诺数组中的项设置为可选 2022-01-01
- Electron 将 Node.js 和 Chromium 上下文结合起来意味着 2022-01-01
- 直接将值设置为滑块 2022-01-01
- html表格如何通过更改悬停边框来突出显示列? 2022-01-01
- 如何使用JIT在顺风css中使用布局变体? 2022-01-01
- Vue 3 – <过渡>渲染不能动画的非元素根节点 2022-01-01
- 自定义 XMLHttpRequest.prototype.open 2022-01-01