新的jQuery的API链中允许的同步操作? [英] New jQuery API to allow synchronous operations during chaining?

查看:99
本文介绍了新的jQuery的API链中允许的同步操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除非我弄错了,因为jQuery的1.5,你能以某种方式做链接和插入命令来让它等待previous操作完成。换言之,代替这样的:

Unless I'm mistaken, since jQuery 1.5, you can somehow do chaining and insert a command to have it wait for the previous operation to complete. In other words, instead of this:

$('#something').animate({opacity: 0}, 500, function() { $('#something').hide(); });

您可以这样做:

$('#something').animate({opacity: 0}, 500).waitForIt().hide();

当然,我可能是错的。我知道有延迟()和动画()一个排队的选项,但我认为那些适用于QUEUEABLE FX而已,而不是一个射击状显示/隐藏。

Of course I could be wrong. I know there's delay() and a "queue" option for animate(), but I think those apply to queueable FX only, not one-shots like show/hide.

推荐答案

你在哪里找到该功能的提?据我所知,即使是jQuery的1.5的递延对象不能与<,很容易使用A HREF =htt​​p://api.jquery.com/animate/相对=nofollow>动画()。

Where did you find mention of that feature? To my knowledge, even jQuery 1.5's deferred objects cannot be used that easily with animate().

幸运的是,丹赫伯登写道一些非常好的code 使得语法清晰。用他的解决方案,你可以写这样的:

Fortunately, Dan Heberden wrote some very nice code that makes the syntax clearer. Using his solution, you can write something like:

var $something = your$('#something');
$.when($something.animate({opacity: 0}, 500)).done(function() {
    $something.hide();
});

这当然是很多不是简单地传递一个回调动画()放在首位更详细。

编辑: 的jQuery 1.6 现在本身所实现该功能。实际上,你可以这样做:

jQuery 1.6 now implements that feature natively. You can actually do:

var $something = $('#something');
$.when($something.animate({opacity: 0}, 500)).done(function() {
    $something.hide();
});

,如果你已经在使用延迟的对象,并希望合并动画到您的AJAX请求流仍然是矫枉过正简单的动画,但非常有用的。

That's still overkill for simple animations, but quite useful if you're already using deferred objects and want to incorporate animations into your AJAX request flow.

这篇关于新的jQuery的API链中允许的同步操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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