簡體   English   中英

在IE中打開文件選擇對話框時的setTimeout / setInterval

[英]setTimeout/setInterval when file selection dialog is open in IE

當文件輸入的“選擇文件”對話框窗口打開時,Internet Explorer(11)似乎阻止了觸發setTimeout / setInterval回調。 在對話框打開時是否有任何可靠的觸發重復事件(例如心跳)的方法?

除了可能使用網絡工作者之外,沒有。

JavaScript是單線程和本機彈出窗口,例如alertconfirm和文件對話框窗口阻止主線程上的所有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);

請注意, 只有IE 10和11支持requestAnimationFrame

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM