加入在JavaScript 2“主题” [英] Join 2 'threads' in javascript

查看:130
本文介绍了加入在JavaScript 2“主题”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个Ajax调用关取(有回调),然后其他一些code运行在此期间。我怎样才能有第三个功能,当第一2的完成,将调用。我敢肯定,它很容易与轮询(setTimeout和再检查一些变量),但我宁愿一个回调。

If I have an ajax call off fetching (with a callback) and then some other code running in the meantime. How can I have a third function that will be called when both of the first 2 are done. I'm sure it is easy with polling (setTimeout and then check some variables) but I'd rather a callback.

这可能吗?

推荐答案

您可以只给出相同的回调到这两个AJAX调用,并在此期间,你的另一code运行时,使用一个变量来跟踪他们的联合进步,然后将它们链接到一个回调象下面这样:

You could just give the same callback to both your AJAX call and your other code running in the meantime, use a variable to track their combined progress, then link them to a callback like below:

// Each time you start a call, increment this by one
var counter = 0;

var callback = function() {
    counter--;
    if (counter == 0) {
        // Execute code you wanted to do once both threads are finished.
    }
}

这篇关于加入在JavaScript 2“主题”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆