all articles

setTimeout is triggered after after history.back()

2015-06-28 @sunderls

iOS safari js

description

sometimes if we want to call history.back(), and change location to somewhere else after 0.5s as a fallback, we may simply do something like:

history.back();
setTimeout(function(){
    // do something else if back() is not success
}, 500);

well it is not working well. On IOS Safari, it won't work.

workaround

we have to use setInterval to count the time by ourself.

history.back();
var time = 0;
var timer = setInterval(function(){
    time += 100;
    if (time >= 500) {
          // do something else
          clearTimeout(timer);
    }

}, 100);