我有两个 JS 函数。一个调用另一个。在调用函数中,我想调用另一个函数,等待该函数完成,然后继续。因此,例如/伪代码:
function firstFunction(){
    for(i=0;i<x;i++){
        // do something
    }
};
function secondFunction(){
    firstFunction()
    // now wait for firstFunction to finish...
    // do something else
};
我想出了这个解决方案,但不知道这是否是一种明智的方法。
var isPaused = false;
function firstFunction(){
    isPaused = true;
    for(i=0;i<x;i++){
        // do something
    }
    isPaused = false;
};
function secondFunction(){
    firstFunction()
    function waitForIt(){
        if (isPaused) {
            setTimeout(function(){waitForIt()},100);
        } else {
            // go do that thing
        };
    }
};
这是合法的吗?有没有更优雅的方法来处理它?也许用jQuery?