How to use “queue” or “deferred” in what condition? What are their designing purpose?

不打扰是莪最后的温柔 提交于 2019-12-01 03:53:09

问题


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


回答1:


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


来源:https://stackoverflow.com/questions/8119208/how-to-use-queue-or-deferred-in-what-condition-what-are-their-designing-pur

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!