什么是延期对象? [英] What are deferred objects?

查看:62
本文介绍了什么是延期对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

jQuery 1.5添加了延迟对象".它们是什么,它们的作用到底是什么?

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

推荐答案

延迟对象

从jQuery 1.5开始,Deferred对象提供了一种将多个回调注册到自我管理的回调队列中,酌情调用回调队列以及中继任何同步或异步函数的成功或失败状态的方法.

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.done()
    • 添加要在解析Deferred对象时调用的处理程序.
    • 添加当Deferred对象被拒绝时要调用的处理程序.
    • 确定是否推迟了对象.
    • 确定是否已解析延迟的对象.
    • 拒绝一个Deferred对象,并使用给定的args调用所有failCallbacks.
    • 拒绝一个Deferred对象,并使用给定的上下文和参数调用所有failCallbacks.
    • 解析一个Deferred对象,并使用给定的args调用任何doneCallbacks.
    • 解析一个Deferred对象,并使用给定的上下文和参数调用所有doneCallbacks.
    • 添加解析或拒绝Deferred对象时要调用的处理程序.

    推迟执行:

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

    似乎现有的ajax()方法回调可以被链接而不是在设置中声明:

    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"); });
    


    工作示例摘自Eric Hynds博客文章: http://jsfiddle.net/ehynds/Mrqf8/

    jqXHR

    从jQuery 1.5开始,$ .ajax()方法返回jXHR对象,该对象是XMLHTTPRequest对象的超集.有关更多信息,请参见$ .ajax条目的"jXHR"部分

    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

    来自 JQUERY 1.5已发布:

    延迟对象

    随着Ajax的改写 模块引入了新功能 这也是公开的 可用:延迟对象.这 API使您可以使用return 可能不是立即的值 现在(例如返回结果 来自异步Ajax请求). 此外,它还为您提供了能力 附加多个事件处理程序 (以前没有的东西 可以在Ajax API中使用.)

    Along with the rewrite of the Ajax module a new feature was introduced which was also made publicly available: Deferred Objects. This API allows you to work with return values that may not be immediately present (such as the return result from an asynchronous Ajax request). Additionally it gives you the ability to attach multiple event handlers (something that wasn’t previously possible in the Ajax API).

    此外,您可以自己制作 使用暴露的延迟对象 jQuery.Deferred.更多信息 有关此API的信息,请参见 延迟对象文档.

    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)撰写了一篇不错的文章 在jQuery中使用延迟的教程 1.5 .

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

    这篇关于什么是延期对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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