jQuery 1.5 adds "Deferred Objects". What are they, and what exactly do they do?

推荐答案

Deferred Object

As of jQuery 1.5, the Deferred object provides a way to register multiple callbacks into self-managed callback queues, invoke callback queues as appropriate, and relay the success or failure state of any synchronous or asynchronous function.

Deferred Methods:

Deferred In Action:

$.get("test.php").done(
    function(){ alert("$.get succeeded"); }
);

$.get("test.php")
    .done(function(){ alert("$.get succeeded"); })
    .fail(function(){ alert("$.get failed!"); });

And it seems that the existing ajax() method callbacks can be chained rather than declared in the settings:

var jqxhr = $.ajax({ url: "example.php" })
    .success(function() { alert("success"); })
    .error(function() { alert("error"); })
    .complete(function() { alert("complete"); });

Working Example From Eric Hynds blog post: http://jsfiddle.net/ehynds/Mrqf8/


jqXHR

As of jQuery 1.5, the $.ajax() method returns the jXHR object, which is a superset of the XMLHTTPRequest object. For more information, see thejXHR section of the $.ajax entry


From JQUERY 1.5 RELEASED:

DEFERRED OBJECTS

随着Ajax的重写

Additionally you can make your own deferred objects using the exposed jQuery.Deferred. More information about this API can be found in the Deferred Object documentation.

Eric Hynds has written up a good tutorial on Using Deferreds in jQuery 1.5.

Jquery相关问答推荐

JQuery AJAX数据表保留前导零

使用 shell 脚本判断 json 数组响应是否具有特定用户名和状态的 jq 命令

jQuery:在mousemove事件期间检测按下的鼠标按钮

Jquery $(this) 子 Select 器

jQuery 1.4.1 中缺少 JSON 字符串化?

在 select2 中使用 AJAX 进行标记

数据表警告(表 id = 'example'):无法重新初始化数据表

SCRIPT7002:XMLHttpRequest:网络错误 0x2ef3,由于错误 00002ef3 无法完成操作

JQuery 仅在 Rails 4 应用程序中的页面刷新时加载

在网络浏览器中,onblur 和 onfocusout 有什么区别?

找到下一个匹配的sibling 姐妹的有效,简洁的方法?

jQuery $.cookie 不是一个函数

jQuery ajax 在 asp.net mvc 中上传文件

offsetTop 与 jQuery.offset().top

更改占位符文本

从 Select 元素中获取选定的选项

如何隐藏 Twitter Bootstrap 下拉菜单

jQuery从字符串中删除'-'字符

在jQuery中获取CSS规则的百分比值

textarea 的 val() 与 text()