为什么JQuery.getJSON()具有成功函数和完成函数? [英] Why does JQuery.getJSON() have a success and a done function?

查看:75
本文介绍了为什么JQuery.getJSON()具有成功函数和完成函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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).

来自延迟对象文档:

在JavaScript中,通常会调用可选接受的函数 在该函数中调用的回调.例如, jQuery 1.5之前的版本,例如异步进程 jQuery.ajax()接受要在一段时间内调用的回调 成功,错误和ajax请求完成后不久,就可以了.

In JavaScript it is common to invoke functions that optionally accept callbacks that are called within that function. For example, in versions prior to jQuery 1.5, asynchronous processes such as jQuery.ajax() accept callbacks to be invoked some time in the near-future upon success, error, and completion of the ajax request.

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()具有成功函数和完成函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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