我有两个功能如下:
var doSomething = function() {
// first check if user wants to proceed and some other restrictions
checkIfReallyShouldProceed();
console.log('ok, proceed');
// proceed and do something
}
var checkIfReallyShouldProceed = function() {
var check = confirm('really proceed?');
if(!check){
//stop executing doSomething
}
}
doSomething();
如果用户未确认我想从doSomething返回。当然,我可以将check变量的结果返回到doSomething,并具有类似
的内容if(!checkIfReallyShouldProceed()){
return;
}
那里,但我希望被调用的函数停止执行调用函数。这是可能的,如果是的话,怎么样?
答案 0 :(得分:-1)
对此类条件过程进行if
条件:
var doSomething = function() {
if (checkIfReallyShouldProceed()){
return true; // This will stop the doSomething function from executing
}
console.log('ok, proceed');
}
var checkIfReallyShouldProceed = function() {
return confirm('really proceed?'); // returns true/false
}
doSomething();
在checkIfReallyShouldProceed
函数中,返回用户是否要继续。在doSomething
中,如果被调用的方法返回true