[英]Return from calling function in Javascript
I have two function like below: 我有如下两个功能:
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();
If the user does not confirm I want to return from doSomething. 如果用户不确定,我想从doSomething返回。 Of course I could return the result of the check variable to doSomething and have something like
当然,我可以将check变量的结果返回给doSomething并得到类似
if(!checkIfReallyShouldProceed()){
return;
}
there, but I want the called function to stop the calling function from executing. 在那里,但我希望被调用函数停止执行调用函数。 Is this possible and if so, how?
这可能吗?如果可以,怎么办?
An if
condition is made for this type of conditional procedure: 对于这种类型的条件过程建立了
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();
In the checkIfReallyShouldProceed
function, return whether or not the user wants to proceed. 在
checkIfReallyShouldProceed
函数中,返回用户是否要继续。 In doSomething
, it will stop executing if the called method returns true
在
doSomething
,如果被调用的方法返回true
,它将停止执行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.