在window.onload中使用回调

时间:2015-06-11 13:32:16

标签: javascript callback

我认为这有一个简单的解决方案,但由于某种原因,我没有得到预期的结果。我的功能看起来像这样:

var functionA = function(callback) {
    loadData(fromURL1); // takes some time
    loadData(fromURL2); // takes some time
    callback();         // Should be called AFTER the loadData() functions are finished
}

var myCallBackFunction = function() {
  // this function is called AFTER functionA() is finished
  alert("All my loaded data from URL1 and URL2");
}

window.onload = function() {
   functionA(myCallBackFunction);
}

不幸的是,上面的callback()函数并没有等待loadData()完成,然后只用空数据调用警报。

我阅读了很多在线示例,但我认为我仍然遗漏了一些显而易见的事情。

1 个答案:

答案 0 :(得分:1)

如果@api.one @api.onchange('facture_ids.total_a_payer') def _invoice_total(self): if self.facture_ids.type=="npay": self.total_invoiced = sum(line.facture_ids.total_a_payer for record in self.browse()) 是异步操作,您可以做两件事:

使用loadData()

$.ajaxComplete()

或链接功能:

var functionA = function(callback) {
    loadData(fromURL1); // takes some time
    loadData(fromURL2); // takes some time
    $.ajaxComplete(function () {
        callback();         // Should be called AFTER the loadData() functions are finished
    });
}