jquery和动画的异步程度如何?
我的脚本遇到了一些不可预测的事故,我开始认为这可能与我使用的动画有关。
我有一个附加了 click() 事件的元素,该事件会触发动画。比如说
$(el).click(function() {
$(this).animate({some properies}, 500);
});
我们稍微扩展一下,
$(el).click(function() {
$(this).stop(true, true);
fancyFunction(this);
$(this).animate({some properies}, 500);
});
我现在添加了对 fancyFunction 的调用,重要的是我停止了对象上正在进行的动画(例如,如果我双击)。另请注意,fancyFunction() 必须确保元素上没有运行任何动画。
我想这可能是我遇到的问题。您能否放心,stop() 在调用下一行之前完成(在本例中为 fancyFunction()),或者 fancyFunction 会在 stop() 启动后立即触发,但不一定完成?如果它的行为像动画类别中的其他功能一样,我想不会。
如果这可能是问题所在,您有什么解决方案?
I'm getting some unpredictable mishaps with my script and I'm beginning to think it might have something to do with the animation I use.
I have one element with the click() event attached that triggers an animation. Something like
$(el).click(function() {
$(this).animate({some properies}, 500);
});
Let's say we extend this a little bit
$(el).click(function() {
$(this).stop(true, true);
fancyFunction(this);
$(this).animate({some properies}, 500);
});
I have now added a call to fancyFunction and the important thing is that I stop ongoing animation on the object (in case I double click for example). Also for the record fancyFunction() MUST be sure that no animation is running on the element.
I think that this might be the problem I have. Can you rest assured that stop() is complete before next line is called, in this case fancyFunction(), or will fancyFunction fire immediately after stop() has started, but not necessarily completed? If it behaves like other functions in the animation categories I guess no.
If this might be the problem, what solutions do you have?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
.stop()
是同步,否超时等...它将在下一条语句之前完成。我认为您的问题很可能出在其他地方,因为您所拥有的将杀死该元素上的所有动画。.stop()
is synchronous, no timeouts, etc...it will complete before the next statement. I think your problem most likely lies elsewhere, since what you have will kill all animations on that element.Javascript 是单线程的,因此在执行 JavaScript 时不可能执行另一段代码。即使动画仍在运行,您的代码此时正在执行这一事实也意味着动画无法在代码运行时实际更改对象。但是,是的,您的停止调用将停止动画运行,并且在 fancyFunction 运行时没有任何动画。
在继续之前你想做的事情。 fancyFunction 是否使用计时器或任何可能延迟处理的东西?如果是这样,请考虑不使用计时器或延迟。我确信您可以做一些不涉及计时器的事情。
Javascript is single threaded so there's no way another piece of code is executing while your javascript is executing. Even if the animations was still running the fact that your code is executing at this point means the animation can't be actually changing the object as your code is running. But, yes your stop call will stop the animation from running, and nothing is animating while fancyFunction is running.
Somethings you'll want to do before moving on. Does fancyFunction use timers or anything that could delay processing? If so consider not using timers or delays. I'm sure there is something you could do that doesn't involve timers.