First of all, it has two pages: example.com/pageA
and example.com/pageB
, it's not a SPA.
How to run a delay task after the page being killed by history.back
?
I tried using setTimeout
but failed to execute the task after history.back
, because the timer
will be clear in stack when page change.
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()
}
In PageA
the count still be 1 instead of 2.
For security reason, your goal to run some code after page being killed will never be supported by the browser, because it's dangerous when some code can run on a different page.
Once the page being reload/killed, all the unfinished tasks will be clear even if in a SharedWorker
.
So, two ways to accomplish your logic: