问题
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