简体   繁体   English

如何确保一项任务仅在另一项任务结束后才发生

[英]How to ensure that one task happens only after another task ends

Don't make it duplicate please. 请不要重复。 I am in middle of a big problem. 我遇到了一个大问题。 I am working on a javascript code with thousands of lines of code. 我正在使用具有数千行代码的javascript代码。 My problem is: 我的问题是:

methodA is called from its own class. 从它自己的类中调用methodA

methodA{  
    doSomething(abc, def, function(obj){  
        obj.getName();  
    });  
}  

doSomething(abc, def, callback){  
// codes....
callback(new XYZ());  
}

In XYZ class constructor I am using a couple of Jquery Deffered objects(with ajax calls). 在XYZ类构造函数中,我正在使用几个Jquery Deffered对象(带有ajax调用)。 So my problem is at obj.getName() I am getting error that this.name is not defined. 所以我的问题是在obj.getName()我收到未定义this.name错误。 So how can I ensure that entire new XYZ() execution ends completely(all ajax calls, callbacks etc.) when the control comes to obj.getName() function ? 那么,当控件涉及obj.getName()函数时,如何确保整个新的XYZ()执行完全结束(所有ajax调用,回调等)?

Thanks 谢谢

You can use function call back for this purpose. 为此,可以使用函数回调。 more can be searched on google by jquery deferred As shown here 可以通过jquery deferred在Google上搜索更多内容, 如下所示

var jqDeferred = doSomething(abc,def,function(obj){

});

jqDeferred.done(function(){
obj.getName();  
})


doSomething(abc,def,callback)
{

}

you can use .when and .then => http://api.jquery.com/jquery.when/ 您可以使用.when和.then => http://api.jquery.com/jquery.when/

methodA{  
    doSomething(abc, def, function(obj){  
        $.when(callback(new XYZ())).then(obj.getName());  
    }); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM