问题描述
getJSON的JQuery文档显示了以下示例:
var jqxhr = $.getJSON( "example.json", function() {
console.log( "success" );
})
.done(function() {
console.log( "second success" );
})
.fail(function() {
console.log( "error" );
})
.always(function() {
console.log( "complete" );
});
成功函数(作为第二个参数传递)与done()函数有什么区别?他们似乎是同一回事.
What's the difference between the success function (passed as the 2nd parameter) the the done() function? They seem to be the same thing.
推荐答案
最初,jQuery异步函数没有返回promise,必须使用回调.
Initially, jQuery asynchronous functions weren't returning promises, you had to use the callback.
然后,他们添加了延迟(承诺)系统,但保留了回调以保持兼容性(并且因为并非所有人都喜欢延迟).
Then they added the deferred (promise) system but kept the callbacks for compatibility (and because not everybody like deferred).
来自延迟对象文档:
jQuery.Deferred()对回调的方式进行了一些增强 被管理和调用.特别是jQuery.Deferred()提供了 提供多个回调的灵活方式,这些回调可以 无论原始回调分派是否具有 已经发生了. jQuery Deferred基于CommonJS Promises/A 设计.
jQuery.Deferred() introduces several enhancements to the way callbacks are managed and invoked. In particular, jQuery.Deferred() provides flexible ways to provide multiple callbacks, and these callbacks can be invoked regardless of whether the original callback dispatch has already occurred. jQuery Deferred is based on the CommonJS Promises/A design.
这篇关于为什么JQuery.getJSON()具有成功函数和完成函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!