Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript stop execution abort or exit

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

Inside function callFunc(), what do I have to write so that execution completely stops? It should not execute frm.submit();

function callFunc()
{
    //stop execution here -- ensure it won't execute fm.submit()
}
like image 739
Rocky111 Avatar asked Mar 23 '12 06:03

Rocky111


2 Answers

Better one is

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

than from any where call this

try
{
    for(var i=0;i<10;i++)
    {
         if(i==5)Abort();
    }
} catch(e){}

For you

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) {}
like image 73
Pranay Rana Avatar answered Oct 20 '22 14:10

Pranay Rana


As you've discovered, aborting JavaScript almost always involves exceptions. If you truly can't change the wrapper, then you might have to resort to something a bit more extreme. One (evil) way to kill the script is to convince the browser that it's taking too long, by running an infinite loop:

function callFunc()
{
    //stop execution here
    var n = 1;
    while (n) {
        n += 1;
    }
}

Modern browsers will let the user kill the script after a while. Granted, it will make your site seem broken, but that should give you the leverage you need to get a better API in place.

If the busy-loop is too extreme, you could replace the simple addition with a plugin-based sleep, or perhaps a synchronous network request that takes an extremely long time, wrapped in its own try/catch safety net.

like image 37
eswald Avatar answered Oct 20 '22 15:10

eswald