关闭选项卡时忽略可见性更改

时间:2017-10-05 04:05:59

标签: javascript html

条件应该是什么,以便在切换选项卡时调用d.fx.pause();,而不是在关闭选项卡时调用(因为在我的情况下它是无用的)。

window.addEventListener("beforeunload", function() {
    /* NOT USED */
    console.log("1: beforeunload, could ask for confirmation", document.hidden);
});

document.addEventListener("visibilitychange", function() {
    /* pause when switching away, but not when unloading */
    const condition = true; // ?
    console.log("2: visibilitychange", document.hidden);
    if (document.hidden && condition) {
      //d.fx.pause();
    }
});

window.addEventListener("unload", function() {
    console.log("3: unload", document.hidden);
});

1 个答案:

答案 0 :(得分:1)

您可以在beforeunload事件中添加一个标记,然后在visibilitychange中进行检查。

e.g。

var unloading = false;
window.addEventListener("beforeunload", function() {
  unloading = true;
  /* NOT USED */
  console.log("1: beforeunload, could ask for confirmation", document.hidden);
});

document.addEventListener("visibilitychange", function() {
  /* pause when switching away, but not when unloading */
  const condition = true; // ?
  console.log("2: visibilitychange", document.hidden);
  if (!unloading && document.hidden && condition) {
    //d.fx.pause();
    console.log("d.fx.pause()");
  }
});

window.addEventListener("unload", function() {
  console.log("3: unload", document.hidden);
});

相关问题