5

This is related to a similar question I have posted about timing issues when tabs are inactive. I was wondering if there is a way to make an inactive tab run all functions at the same speed as if it was active. What would be ideal is some script at the top of the page that applies it to everything in the document.

Sorry forgot to put a link on the related issue to show what it is I am trying to do.

Have placed it below

Error with Javascript / Jquery timing in inactive tabs

Thanks

Gary

Community
  • 1
  • 1
Gary B
  • 143
  • 1
  • 9
  • 1
    Javascript execution speed is never constant. What are you trying to accomplish? – recursive May 06 '13 at 16:04
  • @recursive: Yes, but some browsers completely suspend or dramatically slow down JavaScript execution in inactive tabs. But the question "What are you trying to accomplish?" applies regardless. :-) – T.J. Crowder May 06 '13 at 16:06
  • 1
    @ Gary: As recursive says, what's your actual goal? It may be that there isn't any way to prevent the suspension/slowdown in inactive tabs, but that there's a different way to get to your actual goal. But if you don't share what that is, we can't help you find it. :-) – T.J. Crowder May 06 '13 at 16:08
  • I have used this with good success where i wanted accuracy. There is one example of polling system clock and a more in-depth one for animation related challenges. http://www.sitepoint.com/creating-accurate-timers-in-javascript/ – gillyspy May 06 '13 at 16:10
  • Hi Sorry I forgot to post my related issue. This give more details of what I am trying to do. http://stackoverflow.com/questions/16399057/error-with-javascript-jquery-timing-in-inactive-tabs – Gary B May 06 '13 at 16:28
  • @gillyspy: But that's not relevant when the tab is inactive… – Bergi May 06 '13 at 16:53
  • See duplicate [Are there any standards for (mobile device) web browsers in terms of thread sleeping?](http://stackoverflow.com/questions/10739835/are-there-any-standards-for-mobile-device-web-browsers-in-terms-of-thread-sleepi). The answer is **No**, should I repost it here? – Bergi May 06 '13 at 16:54
  • @Bergi can you be specific in your comment? -- from what we are asked in this question it still applies, because you can use the technique I linked to "catch up" the animation when it becomes active and re-polls the system clock. – gillyspy May 06 '13 at 17:09
  • The problem I have is that the timing of my animation in the post i added is messed up when I leave the tab inactive. This makes the timing of the station being underlined happen too fast. Will this catch up work if the animation is embedded in an iframe? Could I maybe speed up the circle so it keeps up when the screen is inactive? Thanks – Gary B May 06 '13 at 17:13
  • @gillyspy: OK, yes, of course a well-written timer/animation would automatically catch up - you need to do that anyway as `setTimeout` is not reliable. I only meant that this technique cannot be used to hurry up the browser's timing on inactive tabs… – Bergi May 06 '13 at 17:14
  • Ok thats great. I have used the jQuery delay function instead. How would I implement the catch up? thanks – Gary B May 06 '13 at 17:16
  • Make sure to make use of `clearTimeout()` and `clearInterval()` that usually solves issues when changing tabs. I had a slideshow that I built and it fixed the issues with the slides trying to "catch up" when I switched tabs – ddilsaver May 06 '13 at 17:27
  • @GaryB if you're tracking the system clock then you'll know how many animation steps you have missed. If your code is abstracted enough then you'll have the step calculations separate from the display methods and you can "jump" over the skipped frames of animation to where you should be. Time spent in your display delay on a station should be a product of system clock now and will be factored in with time remaining. You could even have an extra,separate animation cycle where you identify "catch up" blocks of a certain size with a different type of animation. – gillyspy May 06 '13 at 17:44
  • A specific example would be a fading "tail" of a different color on the portion of the route that was traversed while the tab was inactive – gillyspy May 06 '13 at 17:44
  • Hi gillyspy. Sorry but im a bit confused. Its not your explanation its just im fairly new to jQuery. I currently have the animation queue of the circle and the delays for the text in another queue. So It sounds that there is a way to get the circle to catch back up with the animation after the page being inactive. How would you do this? thanks – Gary B May 06 '13 at 17:52

1 Answers1

0

I believe that only async events, like setTimeout are delayed for background tabs, I would guess that web workers would not be delayed, so I would suggest using those.

erikvold
  • 15,988
  • 11
  • 54
  • 98