I am trying to redirect a page after [2] minute, even if the page is not active. This is mainly for mobile users, who will likely leave the page, resulting in the countdown timer stopping.
When a mobile user lands on the page a message is displayed that the content will be ready in 2 min. Most won't wait around and close the window, only to return 2 min. later and still having to wait for the entire 2 min...
I am currently using the below code, but it doesn't seem to work
<script>
var timer = setTimeout(function() {
window.location='http://example.com'
}, 120000);
</script>
Thanks much! Chris
You cannot redirect after two Minutes. But you can redirect if the user gets it back into foreground after at least two minutes:
var before=(new Date()).getMinutes();
setInterval(function() {
if((new Date()).getMinutes()-before >= 2){
window.location='http://example.com';
}
}, 1000);//compare each second
If you want it to work when the page was closed too, you ight store it in localStorage:
localStorage.setItem("before",localStorage.getItem("before") || (new Date()).getMinutes());
setInterval(function() {
if((new Date()).getMinutes()-localStorage.getItem("before") >= 2){
window.location='http://example.com';
}
}, 1000);//compare each second
Note that this wont work at full hour change so you might recode it so that its based on getTime() , but well thats up to you...