jQuery Animate Step功能 [英] jQuery Animate Step function

查看:78
本文介绍了jQuery Animate Step功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了step函数的一些问题。我必须在STEP函数中调用一个复杂的函数,但我试图限制调用次数以提高性能。为了使事情变得更复杂,我将动画2个属性。

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();
}});

该函数考虑OBJ的位置/尺寸,然后计算大约10的新尺寸和位置其他元素,所以间隔必须相当流畅(可能每隔3或4次迭代?)什么是限制复杂函数调用次数的最佳方法?
谢谢:)

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 :)

推荐答案

最简单的方法是使用计数器。

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);

使用此方法,您还应该在 .promise()中运行它.done()回调。 (更新以反映)

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

这篇关于jQuery Animate Step功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆