jQuery.animate “too much recursion” from complete callback
Posted by stephen on June 11th, 2010I was working on a class for sequencing a set of DOM elements, and ran into a snag using the animate function like this:
$.fn.animate.apply($item, this.transitionOutArgs);
My animate args (properties, options) looked something like this:
[{opacity:.1}, {duration:1500, complete: function(){ $(this).css("display","none"); }}]
Firebug kept saying “too much recursion”, and only after the second time the code was run. After a bit of head scratching and some digging into the jQuery code, I saw that it copies the options.complete callback and replaces it with a function that calls the old callback. So, after passing in my complete callback the first time, this.transitionOutArgs[1].complete() was reassigned to the proxy-ish function, and each time after, the proxy-ish function was calling itself.
It’d be nice if jQuery created another callback, such as options.$complete(), that proxies for our explicitly defined options.complete callback, instead of reassigning it, but they may have their reasons. To work around this, I just copied the args before passing them to the function.
$.fn.animate.apply($item, $.extend(true, [], this.transitionOutArgs));
If you’re declaring the transitionOutArgs in the same scope as where the animate function is called, there is no need to copy it, since it is not being referenced and reused. That is the typical use case.
Recent Comments