zoukankan      html  css  js  c++  java
  • Jquery异步 Deferred Object

    Deferred Object

    function f1() {
    var dfd = $.Deferred();
    setTimeout(function () {
    dfd.resolve();// resolved, rejected, or still in progress.
    }, 500);
    return dfd.promise();
    }

    f1().done(function () {
    console.log("Hello World !")
    }).fail(function () {
    console.log("Error")
    });

    var wait = function (dtd) {
    setTimeout(function () {
    dtd.resolve();//表示处理结束, 调用后立即执行.done方法
    //dtd.reject();//标识处理失败,调用后立即执行fail方法
    }, 500);
    return dtd.promise();
    };
    //使用$.when()为普通操作添加回调函数 为多个操作指定回调函数
    //$.when(deferred, deferred,...):多有的延迟对象都调用了resolve()时才执行done;只要有一个延迟对象调用了reject()就会执行fail()
    //如果参数不是延迟对象就会跳过该参数并代表成功,如果参数都不是延迟对象,也会成功,会执行done
    //成功或失败的回调函数的参数对应when中的参数,如果参数是延迟对象回调函数得到undefined,不是延迟对象得到参数值。
    //done中的方法可以接受参数由when调用的方法返回
    var dtd = $.Deferred();
    $.when(wait(dtd), wait(dtd))
    .done(function (p1, p2) {
    console.log("when 多个方法执行成功 ")
    })
    .fail(function () {
    console.log("Error")
    });
    //还可以直接在wait对象上部署deferred接口。
    $.Deferred(wait)
    .done(function () {
    console.log("Success ")
    })
    .fail(function () {
    console.log("Error")
    });

    // 为了省事,可以把done()fail()合在一起写,这就是then()方法。

    $.Deferred(wait)
    .then(function () {
    console.log("Success ")
    }, function () {
    console.log("Error")
    }).always(function () {
    console.log("总是执行的方法");
    });

    /* 如果then()有两个参数,那么
    第一个参数是done()方法的回调函数,
    第二个参数是fail()方法的回调方法。
    如果then()
    只有一个参数,那么等同于done()*/

    deferred.always()

    Add handlers to be called when the Deferred object is either resolved or rejected.

    deferred.done()

    Add handlers to be called when the Deferred object is resolved.

    deferred.fail()

    Add handlers to be called when the Deferred object is rejected.

    deferred.notify()

    Call the progressCallbacks on a Deferred object with the given args.

    deferred.progress()

    Add handlers to be called when the Deferred object generates progress notifications.

    deferred.reject()

    Reject a Deferred object and call any failCallbacks with the given args.

    deferred.rejectWith()

    Reject a Deferred object and call any failCallbacks with the given context and args.

    deferred.resolve()

    Resolve a Deferred object and call any doneCallbacks with the given args.

    deferred.then()

    Add handlers to be called when the Deferred object is resolved, rejected, or still in progress.

    jQuery.Deferred()

    A factory function that returns a chainable utility object with methods to register multiple callbacks into callback queues, invoke callback queues, and relay the success or failure state of any synchronous or asynchronous function.

    jQuery.when()

    Provides a way to execute callback functions based on one or more objects, usually Deferred objects that represent asynchronous events.

    .promise()

    Return a Promise object to observe when all actions of a certain type bound to the collection, queued or not, have finished.





  • 相关阅读:
    java如何编写多线程
    Java调用dll动态库
    HashMap源码解析
    LinkedList源码解析
    ArrayList源码解析
    springboot配置cxf
    java生成二维码
    原生js--跨域消息传递
    原生js--应用程序存储和离线web应用
    原生js--userData
  • 原文地址:https://www.cnblogs.com/Full--Stack/p/8041184.html
Copyright © 2011-2022 走看看