jQuery Animate Step function

≡放荡痞女 提交于 2019-12-23 03:01:56

问题


I've run into a bit of an issue with the step function. I have to call a complex function within the STEP function, but am trying to limit the number of calls to improve performance. To make things more complicated, I'm animating 2 properties.

obj.animate({'width':newWidth+px,'height':newHeight+px},
{duration:time,queue:false,step:function(now,fx){
   complicatedFunction();
}});

The function accounts for the positions/dimensions of the OBJ, then calculates new dimensions and positions for roughly 10 other elements, so the intervals have to be fairly fluid (perhaps every 3rd or 4th iteration?) What would be the best way to limit the number of times complicatedFunction is called? Thanks :)


回答1:


The simplest would be to use a counter.

var counter = 0;

obj.animate({'width':newWidth+px,'height':newHeight+px},
{duration:time,queue:false,step:function(now,fx){
   if (counter % 4 === 0) { // run function every 4 steps
       complicatedFunction();
   }
   counter++;
}}).promise().done(complicatedFunction);

Using this method, you should also run it in the .promise().done() callback. (updated to reflect)



来源:https://stackoverflow.com/questions/12415199/jquery-animate-step-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!