如何使用“队列"或“延期"在什么情况下?他们的设计目的是什么? [英] How to use "queue" or "deferred" in what condition? What are their designing purpose?

查看:22
本文介绍了如何使用“队列"或“延期"在什么情况下?他们的设计目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I am confused,is "queue" for animations and deferred for "ajax"? Could someone tell me some typical examples?

解决方案

You're mostly correct.

"deferred objects" can be used for processing asynchronous events - you initiate an action and then register a callback which will be invoked when the action has completed. This includes AJAX, although there are plenty of other uses too.

jQuery queues are indeed primarily used to maintain a queue of (animation) functions to be called in sequence, and .queue() specifically is used to add your own function into an animation queue.

To further complicate matters animations can also trigger asynchronous callbacks when they complete. The "traditional" way of doing this was to supply a callback to the animation function, but with modern jQuery if you call .promise() on a jQuery object you get a deferred object which will be resolved when any existing animations on every element within that object are completed:

$('#id1,#id2').slideUp().promise().done(function() {
    // this will be called when the animations are complete
});

这篇关于如何使用“队列"或“延期"在什么情况下?他们的设计目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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