我有一个函数:
function myfunction() {
if (a == 'stop') // How can I stop the function here?
}
JavaScript中是否有类似exit()的东西?
我有一个函数:
function myfunction() {
if (a == 'stop') // How can I stop the function here?
}
JavaScript中是否有类似exit()的东西?
当前回答
输入任何抛出错误的随机命令,例如:
exit
or
die:-)
其他回答
如果您正在寻找一个脚本,以避免在发现一些错误时提交表单,此方法应该有效
function verifyData(){
if (document.MyForm.FormInput.value.length == "") {
alert("Write something!");
}
else {
document.MyForm.submit();
}
}
将Submit Button类型更改为Button
<input value="Save" type="button" onClick="verifyData()">
希望这对你有所帮助。
这样的:
function myfunction()
{
if (a == 'stop') // How can I stop working of function here?
{
return;
}
}
显然你可以这样做:
function myFunction() {myFunction:{
console.log('i get executed');
break myFunction;
console.log('i do not get executed');
}}
通过使用标签查看块作用域:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/label
我还看不出有什么不好。但这似乎不是一种常用的用法。
推导出这个答案:JavaScript等价于PHP的die
我不喜欢回答那些不是真正解决方案的问题……
...但当我遇到同样的问题时,我采取了以下解决方案:
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)
}
}
希望对大家有用。
退出();可以用来进行下一次验证。