jQuery中首先出现的.always()或.then()回调是什么? [英] What comes first .always() or .then() callbacks in jQuery?

查看:351
本文介绍了jQuery中首先出现的.always()或.then()回调是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您的函数同时具有.then.always回调,哪个将首先执行?

If you have a function which has both .then and .always callbacks, which one will get executed first?

推荐答案

来自 deferred.resolve() 文档:

延迟解决后,任何由添加的doneCallbacks deferred.then()或deferred.done()会被调用.执行回调 按照它们的添加顺序.

When the Deferred is resolved, any doneCallbacks added by deferred.then() or deferred.done() are called. Callbacks are executed in the order they were added.

以下示例:

var $logger = $("#logEntry");
function addLog(content){
   $logger.append($("<li/>").html(content));
}

var newPromise = $.Deferred();

$.when(newPromise).done(function() {
    addLog("1st $when().done!");
});

newPromise.then(function() {
    addLog("1st then!");
}).always(function() {
    addLog("1st always!");
}).done(function() {
    addLog("1st done!");
}).done(function() {
    addLog("2nd done!");
}).always(function() {
    addLog("2nd always!");
}).then(function() {
    addLog("2nd then!");
});

$.when(newPromise).done(function() {
    addLog("2nd $when().done!");
});

addLog("Resolving promise!");

newPromise.resolve();

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<ul id="logEntry"></ul>

这篇关于jQuery中首先出现的.always()或.then()回调是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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