Javascript - 在函数1中调用函数2,然后继续

时间:2012-10-06 18:28:19

标签: javascript function

有可能吗?这该怎么做?点击按钮时,我拨打function1。如果条件为真(i==1),则function1暂停,并且仅在function2完全执行后才执行以下代码。例如:

function1(i){
    //some code here
    if(i == 1){
        function2(i);  // call function2 and waits the return to continue
    }
    //the following code
}

function2(i){
    //do something here and returns
}

1 个答案:

答案 0 :(得分:0)

你走了:

function1(i){
    //some code here
    if(i == 1){
        function2(i);  // call function2 and waits the return to continue
    }
    //the following code
}

function2(i){
    //do something here and returns
}

如果您的意思是function2实际上是以某种方式异步:

function1(i){
    //some code here
    if(i == 1){
        // call function2 and waits for return to continue
        function2(i, function() {
            // the following code
        });  
    }
    else {
        //the following code
    }
}

function2(i, callback){
    //do something async here and return when complete
    setTimeout(callback, 1000);
}