【问题标题】:How do I prevent browser dialogs from blocking the UI timers如何防止浏览器对话框阻止 UI 计时器
【发布时间】:2015-06-25 11:03:25
【问题描述】:

我想在 html 中设置一个计数器。默认情况下,警告、关闭警告等浏览器对话框会阻止计数器执行。我该如何防止呢?我的代码有点像下面给出的 sn-p:

var count = 30;
var counter = setInterval(timer, 1000);

function timer() {
  count = count - 1;
  if (count <= 0) {
    clearInterval(counter);

    return;
  }

  document.getElementById("timer").innerHTML = count + " secs";
}
window.onbeforeunload = function() {
  return "I am blocking counter"
}
<span id="timer">...</span>
<button type="button" name="name" value="caption" onclick="alert('block this')">block counter</button>

jsfiddle

【问题讨论】:

    标签: javascript html multithreading dom-events counter


    【解决方案1】:

    Javascript 对话框总是 io 阻塞的,你唯一的办法就是编写你自己的对话框。

    因此,您将弹出一个放置在页面顶部的 div 容器,而不是弹出一个 io 阻塞警告框。

    你无法避免 onbeforeunload 对话框。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-16
      • 1970-01-01
      • 2011-06-03
      • 1970-01-01
      相关资源
      最近更新 更多