简体   繁体   English

何时使用jQuery动画承诺而不是回调?

[英]When to use jQuery animation promises instead of the callback?

I have the following query. 我有以下查询。 When to use this: 何时使用此:

$('#box').hide(2000).promise().done(function(){
     $('#output').text('Box is hidden');
});

instead of this: 而不是这个:

$('#box').hide(2000,function(){
     $('#output').text('Box is hidden');
});

What are some useful scenarios for using the .promise() method with jQuery animations? .promise()方法与jQuery动画一起使用有哪些有用的场景?

Thank you 谢谢

If you're just animating one item, there is little reason to use a promise over a direct callback. 如果您只是为一个项目制作动画,则没有理由在直接回调中使用承诺。 In this particular case, promises are more useful when you're trying to coordinate multiple different async operations (which is where promises are generally the most useful). 在这种特殊情况下,当您尝试协调多个不同的异步操作(承诺通常最有用的地方)时,promises会更有用。

Suppose you had a whole bunch of items you were hiding and you wanted ONE callback when they were all done. 假设你有一大堆你隐藏的物品,当你完成所有这些时你想要一个回调。 Then, this would do exactly that: 然后,这将完成:

$('.items, .boxes').hide(2000).promise().then(function(){
     $('#output').text('All hidden');
});

Or, suppose you wanted to know when multiple different animations were done so you needed to coordinate multiple actions. 或者,假设您想知道何时完成了多个不同的动画,因此您需要协调多个动作。 Promises have built-in features for that which are more work to hand-code without promises: Promise有内置的功能,更多的工作是手工编码而没有承诺:

var p1 = $('.items, .boxes').hide(2000).promise();
var p2 = $('.sliders').slideUp(2500).promise();
var p3 = $('.openers').slideDown(1500).promise();
$.when(p1, p2, p3).then(function() {
    // all are done here
});

If you want to hand code that without promises, then you will have to maintain a counter and, in each separate callback, check the counter to see if they are all done. 如果你想手持没有承诺的代码,那么你将不得不维护一个计数器,并在每个单独的回调中,检查计数器,看看它们是否全部完成。 It's a lot more code. 这是更多的代码。 Now, if you there are then errors to deal with or multiple other operations chained onto this, any option without callbacks or without some async supporting library quickly becomes a real pain to hand code. 现在,如果你有处理错误或链接到其上的多个其他操作,任何没有回调或没有一些异步支持库的选项很快就会成为手动代码的真正痛苦。 That is why promises were invented. 这就是为什么承诺被发明的原因。

Or, even extend beyond an animation, imagine you want to coordinate both an animation and an ajax call (which you have no idea how long it will take): 或者,甚至超出动画范围,想象你想要协调一个动画和一个ajax调用(你不知道需要多长时间):

var p1 = $('.items, .boxes').hide(2000).promise();
var p2 = $.ajax(...);
$.when(p1, p2).then(function() {
    // both are done here
});

Here's a demo of the differences in notifications. 这是一个关于通知差异的演示。 If you press "Reset", then press "Callbacks", you will see that you get 5 completion notifications. 如果您按“重置”,然后按“回拨”,您将看到您收到5个完成通知。 If you press "Reset" and then press "Promises", you will see that you get 1 completion notification when they are all done. 如果您按“重置”然后按“承诺”,您将看到完成后会收到1个完成通知。

 // configure logging log.id = "results"; $("#runPromises").click(function() { $('.items, .boxes').hide(2000).promise().then(function(){ log("got done notification") }); }); $("#runCallbacks").click(function() { $('.items, .boxes').hide(2000, function(){ log("got done notification") }); }); $("#reset").click(function() { $(".items, .boxes").show(); $("#results").empty(); }); 
 .items { height: 50px; width: 200px; background-color: red; margin-top: 10px; } .boxes { height: 30px; width: 30px; background-color: blue; margin-top: 10px; } #results { margin-top: 30px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script src="http://files.the-friend-family.com/log.js"></script> <button id="runCallbacks">Callbacks</button> <button id="runPromises">Promises</button> <button id="reset">Reset</button> <div> <div class="items"></div> <div class="items"></div> <div class="items"></div> <div class="boxes"></div> <div class="boxes"></div> </div> <div id="results"></div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM