首先,它有两个页面:example.com/pageA
和example.com/pageB
,它不是SPA。
如何在history.back
杀死页面后运行延迟任务
我尝试使用setTimeout
,但在history.back
之后执行任务失败,因为页面更改时,堆栈中的timer
将是透明的。
var a = 1
function refresh() {
// <p id="count"></p>
count.innerText = a
}
function foo() {
a += 1
refresh()
}
// called in PageB
function goBackA() {
setTimeout(foo, 1000)
history.back()
}
在PageA
中,计数仍然是1而不是2。
发布于 2019-07-25 23:25:38
出于安全原因,浏览器将永远不支持在页被杀死后运行一些代码的目标,因为当某些代码可以在不同的页面上运行时,这是危险的。
一旦页面被重新加载/终止,即使在SharedWorker
中,所有未完成的任务也将是明确的。
因此,完成逻辑的两种方法是:
https://stackoverflow.com/questions/57174601
复制