简体   繁体   English

等待执行回调函数

[英]Waiting until callback function is executed

I have a JavaScript class where pass options object in init function. 我有一个JavaScript类,其中init函数中传递options对象。 this options object also contains a callback function options对象还包含一个callback函数

this callback function is a globally defined function which create Ajax Request and updates the contents. 这个callback function是一个全局定义的函数,它创建Ajax Request并更新内容。

After the contents are updated, I want to register some event on the controls returned by the Ajax 更新内容后,我想在Ajax返回的控件上注册一些事件

Is there a way that the Class method (which calls the callback function) waits util the contains are updated and then registers the events? 有没有办法让Class方法(调用回调函数)等待utils包含内容被更新然后注册事件?

In order to do this, you have to pass another callback the first callback. 为此,您必须将另一个回调传递给第一个回调。 Something like: 就像是:

var feedDog = function(callback){
    fedDog = true; //feed the dog

    callback(function(){
        alert("This will alert as a callback to feedDog's callback.");
    });
};

feedDog(function(callback){
    alert("Fed dog");
    callback();
});

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

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