1

I developed a little script that make texts like they were coming from far, with jQuery and animate() function. I have 2 main problem. The first is described here: Jquery font size animation problem in Chrome, too laggy

The second is that I need to loop the animation. The way I did works well, but in FireFox it has been implemented a resource saving mechanism that makes things weird when the tab windows of the broser is in background.

I'm quite sure the problem is known, and concern what you can read on Jquery doc for animate():

Because of the nature of requestAnimationFrame(), you should never queue animations using a setInterval or setTimeout loop. In order to preserve CPU resources, browsers that support requestAnimationFrame will not update animations when the window/tab is not displayed. If you continue to queue animations via setInterval or setTimeout while animation is paused, all of the queued animations will begin playing when the window/tab regains focus. To avoid this potential problem, use the callback of your last animation in the loop, or append a function to the elements .queue() to set the timeout to start the next animation.

...but I cannot understand how to apply a solution to my script.

You can test and play with the code here: http://jsbin.com/ehahoc/7/

Just open the code on a tab, then open another tab from another website and keep the tab with my script running in the background for 30 sec, then come back to the code tab and you will see that all the paragraph are now coming with no coordination or sync.

Many thanks!

Community
  • 1
  • 1
Manny
  • 69
  • 2
  • 10
  • Well, you really cant make that any more smoother. This looks like some hero-area or ad type of animation. You should use flash or GIF for such cases. However, I might be wrong on the contexts. Still, as for your example.. you cannot get that as smooth as flash, ever. – Kalle H. Väravas Sep 15 '11 at 16:42
  • You could come to be what you want using a jquery plugin such like http://nuvutype.nrcstudios.info/ – Awea Sep 15 '11 at 17:52
  • I need to keep this as an overlay layer with transparencies... this is why I haven't use flash, and also to keep it usable for devices that don't use flash. I know Flash is smoother but I'm happy with the result in IE9 and FF, Chrome and IE8 are the problem – Manny Sep 15 '11 at 18:12

1 Answers1

1

I ran into the exact same problem. The bad thing is that you use setInterval/setTimeout together with jQuery's .animate() function.

The jQuery documentation says (source):

Because of the nature of requestAnimationFrame(), you should never queue animations using a setInterval or setTimeout loop. In order to preserve CPU resources, browsers that support requestAnimationFrame will not update animations when the window/tab is not displayed. If you continue to queue animations via setInterval or setTimeout while animation is paused, all of the queued animations will begin playing when the window/tab regains focus. To avoid this potential problem, use the callback of your last animation in the loop, or append a function to the elements .queue() to set the timeout to start the next animation.

At the first glance this sounds more difficult than it actually is. The problem is that jQuery adds more and more animations to the queue, but modern browsers that support requestAnimationFrame do not execute these animations until you re-activate the tab. You can solve this using (or in your case just checking) the .queue() of the element. To understand the .queue(), there's already an article about that.

A simple solution could be to wrap the content of your changeText() function in the following condition - checking the "fx" queue to be empty before adding new animations to it:

var $activeText = $(".container .active-text");

if( $activeText.queue('fx').length == 0 ) {
    // do funky animations, but only if the queue is empty...
}

This should fix the problem in this case. You can find a cloned version of your fixed example here.

Community
  • 1
  • 1
rkallensee
  • 1,994
  • 15
  • 12
  • I upgraded to Jquery 1.6.4 and since this is a problem discussed by the Jquery team they decided to remove, for the moment, the requestAnimationFrame() feature. In any case your soulution is very clever and it seems to work good with requestAnimationFrame(). I will keep it carefully for when requestAnimationFrame() will be back in Jquery. Thanks a lot for having been so detailed in your reply and for the code pasted in jsbin. If you would like to take a look to my other problem related to the same script you can find the link in my 1st post: animation problem in Chrome, too laggy.Many Thanks!! – Manny Sep 28 '11 at 13:03