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>

  • 相关阅读:
    Ext.form.RadioGroup
    Ext:ComboBox实战
    Ext.Ajax.request
    下拉菜单
    为Ext添加下拉框和日期组件
    点击grid单元格弹出新窗口
    好用的sql
    数据库连接池总是断开
    POI 自用API
    String 操作
  • 原文地址:https://www.cnblogs.com/hellogiser/p/jquery-deferred-promise.html
Copyright © 2011-2022 走看看