当满足特定条件时停止 JavaScript 函数

发布于 2024-09-15 17:28:00 字数 181 浏览 2 评论 0原文

我找不到在满足给定条件时部分停止函数的推荐方法。我应该使用 exitbreak 之类的东西吗?

我目前正在使用这个:

if ( x >= 10 ) { return; }  
// other conditions;

I can't find a recommended way to stop a function part way when a given condition is met. Should I use something like exit or break?

I am currently using this:

if ( x >= 10 ) { return; }  
// other conditions;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

南城旧梦 2024-09-22 17:28:00

Return 是退出函数体的方式。您正在使用正确的方法。

我想,根据您的应用程序的结构,您也可以使用 throw。这通常需要将对函数的调用包装在 try / catch 块中。

Return is how you exit out of a function body. You are using the correct approach.

I suppose, depending on how your application is structured, you could also use throw. That would typically require that your calls to your function are wrapped in a try / catch block.

画尸师 2024-09-22 17:28:00

为此使用 return

if(i==1) { 
    return; //stop the execution of function
}

//keep on going

use return for this

if(i==1) { 
    return; //stop the execution of function
}

//keep on going
朦胧时间 2024-09-22 17:28:00

return 语句从函数内的任何位置退出函数:

function something(x)
{
    if (x >= 10)
        // this leaves the function if x is at least 10.
        return;

    // this message displays only if x is less than 10.
    alert ("x is less than 10!");
}

The return statement exits a function from anywhere within the function:

function something(x)
{
    if (x >= 10)
        // this leaves the function if x is at least 10.
        return;

    // this message displays only if x is less than 10.
    alert ("x is less than 10!");
}
猫卆 2024-09-22 17:28:00

在主函数中使用 try...catch 语句,每当您想停止该函数时,只需使用:

throw new Error("Stopping the function!");

Use a try...catch statement in your main function and whenever you want to stop the function just use:

throw new Error("Stopping the function!");
太傻旳人生 2024-09-22 17:28:00

当满足条件时抛出异常以中断函数。

function foo() {
try {
   
    if (xyz = null) //condition
        throw new Error("exiting the function foo");

} catch (e) {
    // TODO: handle the exception here
}

}

throwing the exception when the condition is met to break the function.

function foo() {
try {
   
    if (xyz = null) //condition
        throw new Error("exiting the function foo");

} catch (e) {
    // TODO: handle the exception here
}

}

我不会写诗 2024-09-22 17:28:00

尝试使用 return 语句。它效果最好。当条件满足时,它会停止该功能。

function anything() {
    var get = document.getElementsByClassName("text ").value;
    if (get == null) {
        alert("Please put in your name");
    }

    return;

    var random = Math.floor(Math.random() * 100) + 1;
    console.log(random);
}

Try using a return statement. It works best. It stops the function when the condition is met.

function anything() {
    var get = document.getElementsByClassName("text ").value;
    if (get == null) {
        alert("Please put in your name");
    }

    return;

    var random = Math.floor(Math.random() * 100) + 1;
    console.log(random);
}
著墨染雨君画夕 2024-09-22 17:28:00
if (OK === guestList[3]) {
    alert("Welcome");
    script.stop;
}
if (OK === guestList[3]) {
    alert("Welcome");
    script.stop;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文