简体   繁体   English

从Java调用函数返回

[英]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.

相关问题 从CodeBehind调用Javascript函数并返回值 - Calling Javascript Function From CodeBehind And Return Value 从自动调用JavaScript函数返回值? - Return value from an auto calling JavaScript function? 从javascript函数调用另一个返回false的javascript函数? - Calling from a javascript function another javascript function having return false;? 将 iOS 中被调用函数的结果返回到 JavaScript 中 WkWebView 中的调用函数 - Return result from called function in iOS to calling function in WkWebView in JavaScript 从 function 调用返回以在另一个 function 的变量中使用? javascript - calling return from a function to use in a variable in another function? javascript 在 JavaScript 中调用函数以返回 HTML - Calling a Function in JavaScript to Return HTML 从全局文件调用javascript函数并将其返回以追加到html - Calling a javascript function from a global file and return it to append to html 从Laravel控制器调用Javascript函数并返回数据 - Calling Javascript function from Laravel controller and return data Javascript调用子函数并从函数内部获取返回 - Javascript calling a subfunction and get return from inside a function 将数据从executeAsync返回到调用函数(javascript / mozilla) - return data from executeAsync to the calling function (javascript/mozilla)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM