2017-02-11 189 views
0

在面向对象的语言背景下,我试图学习一些jquery,并围绕异步调用进行打包。我的意图是创建一个JavaScript对象来包含异步API调用的结果,并且能够询问所述对象是否已完成加载。

我一直在尝试使用jQuery的Deferred's来完成它,并且我没有任何问题需要使用片段来处理文档示例,但它不会按照我期望的顺序执行,类。

如何使用$ .Deferred作为成员变量创建javascript对象?

(在我的连接代码超时是模仿API调用延迟)

<!DOCTYPE html> 
<html> 
    <body> 
    <script src="jquery-3.1.1.js"></script> 
    <script> 
     //top level 
     var isdone = false; 
     var def = $.Deferred(); 

     $.when(def).done(function() { 
      var msg = "checking topLevel isdone " + isdone; 
      console.log(msg); 
      $("body").append("<p>" + msg + "</p>"); 
     }) 

     var delayedCall = function() { 
      setTimeout(function() { 
      //resolve after 5 seconds 
      isdone = true; 
      def.resolve(); 
      }, 1000); 
     } 
     delayedCall(); 



     //inside function 
     function DelayedObject() 
     { 
      this.defThis = $.Deferred(); 
      var defVar = $.Deferred(); 
      this.delayedObjCall = function() 
      { 
      setTimeout(function() 
      { 
       //resolve after 5 seconds 
       isdone = true; 
       this.def.resolve(); 
      }, 1000); 
      } 
      this.delayedObjCall(); 
      this.getStatusThis = function() 
      { 
      return this.def; 
      } 
      this.getStatusVar = function() 
      { 
      return this.def; 
      } 
     } 

     delObj = new DelayedObject(); 
     $.when(delObj.getStatusThis()).done(function() { 
      var msg = "checking delObj (this) isdone " + isdone; 
      console.log(msg) 
      $("body").append("<p>" + msg + "</p>"); 
     }); 
     $.when(delObj.getStatusVar()).done(function() { 
      var msg = "checking delObj (var) isdone " + isdone; 
      $("body").append("<p>" + msg + "</p>"); 
      console.log(msg) 
     }); 

     $(window).on("load", function() 
     { 
      var msg = "<p>" + " Page loaded " + "</p>"; 
      $("body").append(msg); 
      console.log(msg); 
     }); 
     </script> 
    </body> 
    </html> 

结果

checking delObj (this) isdone false 

checking delObj (var) isdone false 

Page loaded 

checking topLevel isdone true 
+0

'this.def','this.defThis','defVar' ???你想*一个*延期。 – Bergi

+0

我知道,我在这里包括多个以更好地说明我的问题。可能在代码注释中提到过虽然 – Schaki

回答

1

的一些问题:

  • 你指错了对象/变量在某些地方(this.def不存在,this.defThisdefVar从未使用)

  • this没有在超时回调函数定义的(或者是在window马虎模式),因此需要使用该溶液(多种可能性,例如bind

  • 你永远不解决defVar

  • 您可以使用一个变量isdone,这样就意识到,一旦它被设置为true它仍然true并说一些关于其他承诺。

在这里被校正代码(简化为:省略了文档内容的变化):

var isdone = false; 
 
var def = $.Deferred(); 
 

 
$.when(def).done(function() { 
 
    console.log("checking topLevel isdone " + isdone); 
 
    isdone = false; // set back to false 
 
}); 
 

 
var delayedCall = function() { 
 
    setTimeout(function() { 
 
    isdone = true; 
 
    def.resolve(); 
 
    }, 500); // Half a second 
 
} 
 
delayedCall(); 
 

 
//inside function 
 
function DelayedObject() { 
 
    this.defThis = $.Deferred(); 
 
    var defVar = $.Deferred(); 
 
    this.delayedObjCall = function() { 
 
    setTimeout(function() { 
 
     //resolve after 5 seconds 
 
     isdone = true; 
 
     this.defThis.resolve(); // refer to the correct object 
 
    }.bind(this), 1000); // provide the correct context with bind 
 
    // Also resolve the other deferred: 
 
    setTimeout(function() { 
 
     //resolve after 5 seconds 
 
     isdone = true; 
 
     defVar.resolve(); 
 
    }.bind(this), 1500); //... a bit later 
 
    } 
 
    this.delayedObjCall(); 
 
    this.getStatusThis = function() { 
 
    return this.defThis; // return correct object 
 
    } 
 
    this.getStatusVar = function() { 
 
    return defVar; // return correct object 
 
    } 
 
} 
 

 
delObj = new DelayedObject(); 
 
$.when(delObj.getStatusThis()).done(function() { 
 
    console.log("checking delObj (this) isdone " + isdone); 
 
    isdone = false; // set back to false 
 
}); 
 
$.when(delObj.getStatusVar()).done(function() { 
 
    console.log('checking delObj (var) isdone ' + isdone) 
 
    isdone = false; // set back to false 
 
}); 
 

 
$(window).on("load", function() { 
 
    console.log('Page loaded'); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

+0

bind()是我正在寻找的,thx! – Schaki