0

是否可以?这该怎么做?单击按钮时,我调用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
}
4

1 回答 1

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);
}
于 2012-10-06T18:42:06.763 回答