[英]setTimeout() / setInterval() stops when right click on IE
[英]setTimeout/setInterval when file selection dialog is open in IE
当文件输入的“选择文件”对话框窗口打开时,Internet Explorer(11)似乎阻止了触发setTimeout / setInterval回调。 在对话框打开时是否有任何可靠的触发重复事件(例如心跳)的方法?
除了可能使用网络工作者之外,没有。
JavaScript是单线程和本机弹出窗口,例如alert
, confirm
和文件对话框窗口阻止主线程上的所有JavaScript执行,直到它们关闭。
一种相对昂贵但可靠的方法是滥用requestAnimationFrame,它确实在“select file”对话框打开时触发。
var setRequestAnimationFrameTimeout = function(callback, length) {
var reqId;
var startTime = new Date();
var step = function() {
if ((new Date() - startTime) >= length) {
callback();
} else {
reqId = window.requestAnimationFrame(step);
}
};
step();
return {
cancel: function() {
if (reqId !== undefined) {
window.cancelAnimationFrame(reqId);
}
}
}
};
var setRequestAnimationFrameInterval = function(callback, length) {
var reqId;
var startTime = new Date();
var nextCall = 0;
var step = function() {
var diff = new Date() - startTime;
if (Math.floor(diff / length) === nextCall) {
nextCall += 1;
callback();
}
reqId = window.requestAnimationFrame(step);
};
step();
return {
cancel: function() {
if (reqId !== undefined) {
window.cancelAnimationFrame(reqId);
}
}
}
};
setRequestAnimationFrameTimeout(function() { console.log('timer'); }, 1000);
var i = 0;
setRequestAnimationFrameInterval(function() { console.log('interval', i++); }, 1000);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.