Javascript停止执行中止或退出

Roc*_*111 5 html javascript

if(a.value==1 && b.value==2)
{
    try{callFunc()  }catch(e) {} 
}
frm.submit();
Run Code Online (Sandbox Code Playgroud)

在里面function callFunc(),我必须写什么才能完全停止执行?它不应该执行frm.submit();

function callFunc()
{
    //stop execution here -- ensure it won't execute fm.submit()
}
Run Code Online (Sandbox Code Playgroud)

Pra*_*ana 4

更好的一个是

function Abort()
{
   throw new Error('This is not an error. This is just to abort javascript');
}
Run Code Online (Sandbox Code Playgroud)

比任何地方叫这个

try
{
    for(var i=0;i<10;i++)
    {
         if(i==5)Abort();
    }
} catch(e){}
Run Code Online (Sandbox Code Playgroud)

为你

function callFunc()  
{      
    //stop execution here 
    Abort();

    } 

//code from where you are going to call

try
{
  if(a.value==1 && b.value==2)    
  {        
   callFunc()   
  }    
  frm.submit(); 
}
catch(e) {}
Run Code Online (Sandbox Code Playgroud)

  • 自从我发布该代码以来,您已经对代码进行了相当多的更改,但它仍然不会停止执行。简单地抛出错误不会停止任何事情,它只会执行 catch 块。停止该代码执行的唯一方法是使用 return 语句退出当前执行上下文,因此它必须在函数中,因为您不能在函数外部使用“return”(即您不能逃离全局上下文)。 (3认同)