gpt4 book ai didi

javascript - 关闭选项卡时忽略可见性变化

转载 作者:行者123 更新时间:2023-12-03 03:20:47 27 4
gpt4 key购买 nike

应该什么条件才能在切换选项卡时调用 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);
});

最佳答案

您可以在 beforeunload 事件中添加一个标志,并在 visibilitychange 中检查该标志。

例如

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);
});

关于javascript - 关闭选项卡时忽略可见性变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46577684/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com