4

I have a problem specific to iOS 5 and same code works in ios 4.X and other desktop browsers.

I am using JavaScript to do some stuff every few seconds, now the problem is when I switch to another tab in iPad safari, this script stops working.

When I switch back to this tab, it starts working again.

You can reproduce it on this page, http://www.w3schools.com/js/tryit.asp?filename=tryjs_timing_infinite

Visit this link, click on start counting, and go to some other browser tab.when you come back to this tab after few seconds you will notice counter did not increase when the tab was not active.

I think Apple has done this to improve performance. Can someone suggests a solution to make it work, I am totally stuck on this issue?

Amr Eladawy
  • 4,193
  • 7
  • 34
  • 52
Anil Shahu
  • 97
  • 2
  • 3

4 Answers4

6

If you have a counter, rather than increment it by 1 ever second, store the "start time" and then calculate the count since that start time instead. You would still do this every second, but it would recover from a period of pausing.

<div id="counter"></div>
<script>
var startTime = new Date();

var updateCounter = function () {
    var displayArea = document.getElementById("counter");
    var currentTime = new Date.now();
    var differenceInSeconds = Math.round((currentTime - startTime) / 1000);
    displayArea.innerHTML = differenceInSeconds ;
}

window.setInterval(updateCounter, 1000);
</script>

See the working example on JS Fiddle

UPDATE

I have just tested on an iPad2 running IOS5 and it definitely pauses execution of JavaScript in inactive tabs. You won't be able to prevent this behaviour, you will just have to work with it.

View the test page on JS Fiddle

Fenton
  • 241,084
  • 71
  • 387
  • 401
  • 2
    well that is not my question,i am asking why my JavaScript pauses when tab is not active on ipad(ios 5). – Anil Shahu Nov 02 '11 at 08:01
  • 4
    Your javascript pauses for the reason you guess in your question: performance. As far as I know, there is no way to make the javascript run in background, and unless there is, the only way is to code around it, so that the code can recover from a pause, like Sohnee said. – Adrian Schmidt Nov 02 '11 at 08:04
  • thanks for putting code in your answer,but that is not what i need.the link to counter was just to explain the issue of script pause. can anyone answer my actual question which is how to make JavaScript work when browser tab is not active. – Anil Shahu Nov 02 '11 at 09:08
  • 2
    @AnilShahu The answer (which is included in my answer above) is that you cannot change the browser behaviour and you need to work within the constraints. – Fenton Nov 02 '11 at 09:11
  • Why does the Math.round() call have a second parameter? And why is currentTime just a date instead of Date().getTime()? – Alkanshel Jul 30 '14 at 22:20
  • `Math.round` - that was a typo. Fixed. `new Date()` outperforms `Date().getTime()`, but I have changed it anyway to the even faster `Date.now()`. All three return the same value. – Fenton Jul 30 '14 at 22:25
1

This is nothing new to iOS 5. Mobile Safari has always paused javascript execution for non-active tabs. I know this because of testing I did with keeping a running record of geolocation coordinates a year or two ago in a web page.

Geuis
  • 41,122
  • 56
  • 157
  • 219
  • i think it works in my previous generation ipad With ios 4, anyway can anyone answer my actual question which is how to make JavaScript work when tab is not active.or its not possible at all? – Anil Shahu Nov 02 '11 at 09:08
  • 2
    Like everyone has said, you can't do that. Its not possible because that's not how Mobile Safari works. – Geuis Nov 02 '11 at 09:26
0

I had an issue on iPad when new tab was opened I couldn't catch the 'focus' event so after looking for the right event I found that you can use: "pageshow"

Good Luck!!!

ElizaS
  • 850
  • 1
  • 9
  • 22
0

Why cant you try- check the current browser tab is active or not and based on that condition do your stuff. see this is best solution

Is there a way to detect if a browser window is not currently active?

but i am not sure whether it is usefull for ios browser.

Community
  • 1
  • 1
banny
  • 859
  • 7
  • 12