Javascript 提前退出函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3330193/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Early exit from function?
提问by Simon
I have a function:
我有一个功能:
function myfunction() {
if (a == 'stop') // How can I stop the function here?
}
Is there something like exit()in JavaScript?
exit()JavaScript 中有类似的东西吗?
回答by user113716
You can just use return.
你可以只使用return.
function myfunction() {
if(a == 'stop')
return;
}
This will send a return value of undefinedto whatever called the function.
这将向undefined调用该函数的任何对象发送返回值。
var x = myfunction();
console.log( x ); // console shows undefined
Of course, you can specify a different return value. Whatever value is returned will be logged to the console using the above example.
当然,您可以指定不同的返回值。使用上面的示例,返回的任何值都将记录到控制台。
return false;
return true;
return "some string";
return 12345;
回答by CMCDragonkai
Apparently you can do this:
显然你可以这样做:
function myFunction() {myFunction:{
console.log('i get executed');
break myFunction;
console.log('i do not get executed');
}}
See block scopes through the use of a label: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/label
通过使用标签查看块范围:https: //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/label
I can't see any downsides yet. But it doesn't seem like a common use.
我还没有看到任何缺点。但这似乎并不常见。
Derived this answer: JavaScript equivalent of PHP's die
得出这个答案:JavaScript 等效于 PHP 的死
回答by xlaok
function myfunction() {
if(a == 'stop')
return false;
}
return false;is much better than just return;
return false;比仅仅好得多 return;
回答by Rob
This:
这个:
function myfunction()
{
if (a == 'stop') // How can I stop working of function here?
{
return;
}
}
回答by Rodney Salcedo
Using a little different approach, you can use try catch, with throw statement.
使用稍微不同的方法,您可以使用try catch, with throw 语句。
function name() {
try {
...
//get out of here
if (a == 'stop')
throw "exit";
...
} catch (e) {
// TODO: handle exception
}
}
回答by Awal Istirdja
if you are looking for a script to avoid submitting form when some errors found, this method should work
如果您正在寻找避免在发现错误时提交表单的脚本,则此方法应该有效
function verifyData(){
if (document.MyForm.FormInput.value.length == "") {
alert("Write something!");
}
else {
document.MyForm.submit();
}
}
change the Submit Buttontype to "button"
将提交按钮类型更改为“按钮”
<input value="Save" type="button" onClick="verifyData()">
hope this help.
希望这有帮助。
回答by Seizefire
Using a returnwill stop the function and return undefined, or the value that you specify with the return command.
使用 areturn将停止函数并返回undefined,或使用 return 命令指定的值。
function myfunction(){
if(a=="stop"){
//return undefined;
return; /** Or return "Hello" or any other value */
}
}
回答by Leo
I dislike answering things that aren't a real solution...
我不喜欢回答不是真正解决方案的事情......
...but when I encountered this same problem, I made below workaround:
...但是当我遇到同样的问题时,我做了以下解决方法:
function doThis() {
var err=0
if (cond1) { alert('ret1'); err=1; }
if (cond2) { alert('ret2'); err=1; }
if (cond3) { alert('ret3'); err=1; }
if (err < 1) {
// do the rest (or have it skipped)
}
}
Hope it can be useful for anyone.
希望它对任何人都有用。
回答by Chirag Dineshbhai Ponda
exit(); can be use to go for the next validation.
出口(); 可用于进行下一次验证。
回答by silvster27
If you are using jquery. This should stop the function from bubbling up to so the parent function calling this should stop as well.
如果您使用的是 jquery。这应该会阻止函数冒泡,因此调用 this 的父函数也应该停止。
function myfunction(e)
{
e.stopImmediatePropagation();
................
}

