Join 2 'threads' in javascript

前端 未结 6 1481
时光取名叫无心
时光取名叫无心 2021-01-05 06:44

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

6条回答
  •  感动是毒
    2021-01-05 07:14

    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.
        }
    }
    

提交回复
热议问题