zoukankan      html  css  js  c++  java
  • jquery deferred promise

    <script type="text/javascript">
    /*

    Deferred
    state (then,done, fail, always,pipe, progress, and state)
    change the state (resolve, reject, progress, resolveWith, rejectWith, and progressWith).

    deferred 对象一旦被 resolve 或者 reject 之后,状态是不会改变的。

    deferred.promise()
    The Promise exposes only the Deferred methods needed to attach additional handlers or 
    determine the state (then, done, fail, always, pipe, progress, state and promise), 
    but not ones that change the state (resolve, reject, notify, resolveWith, rejectWith, and notifyWith).

    ----------------------[pending]
    resolve[With]---done [resolved]
    reject[With]---fail [rejected] 
    notify[With]---progress [pending]

    ---------------then(done,fail,progress),always, pipe--->then

    dtd.state():
    pending-resolved/rejected
    */

    /*
    var wait = function(){
    var dtd = $.Deferred(); // 新建一个deferred对象
    var tasks = function(){
    alert("执行完毕!");
       dtd.resolve(); // 改变deferred对象的执行状态
       };
       setTimeout(tasks,5000);
       return dtd.promise();
    };
    $.when( wait())
    .done(function(){ alert("哈哈,成功了!"); })
    .fail(function(){ alert("出错啦!"); });
    */

    var wait = function(dtd){
        //var dtd = $.Deferred(); //在函数内部,新建一个Deferred对象
        var tasks = function(){
          alert("执行完毕!");
          dtd.resolve(); // 改变Deferred对象的执行状态
        };

        setTimeout(tasks,2000);
        return dtd.promise(); // 返回promise对象
      };

    $.Deferred(wait)
      .done(function(){ alert("哈哈,成功了!"); })
      .fail(function(){ alert("出错啦!"); });


    </script>

  • 相关阅读:
    Prometheus09 promtool工具探索
    Prometheus04 prometheus查询语言
    03K8S之service工作方式及使用
    Prometheus08 pushgateway
    prometheus06 grafana可视化
    prometheus05 prometheus使用案例
    Prometheus03 cAdvisorDocker容器监控
    02K8S之自主式Pod
    Prometheus07 blackbox
    javascript实现tab切换效果
  • 原文地址:https://www.cnblogs.com/hellogiser/p/jquery-deferred-promise.html
Copyright © 2011-2022 走看看