我们在前端执行判断时如果条件不成立就让他禁止执行后边的程序 分三种方法:

一)在function里面

(1)return;

(2)return false;

(二)非function方法里面

alert("before error.");

throw SyntaxError();

alert("after error.");

(三)非function方法里面

$("body").on("tap",".go-detail",function(e){

e.preventDefault();

e.stopPropagation();

}

以上是常规的中断执行方法,那么函数调用函数如何在嵌套的函数里面中断执行呢?

1、如果终止一个函数的用return即可,实例如下:

function testA(){

alert('a');

alert('b');

alert('c');

}

testA(); 程序执行会依次弹出’a’,‘b’,‘c’。

function testA(){

alert('a');

return; alert('b');

alert('c');

}

testA(); 程序执行弹出’a’便会终止。

2、在函数中调用别的函数,在被调用函数终止的同时也希望调用的函数终止,实例如下:

function testC(){

alert('c');

return;

alert('cc');

}

function testD(){

testC();

alert('d');

}

testD();

我们看到在testD中调用了testC,在testC中想通过return把alert(‘d’);也终止了,事与愿违return只终止了testC,程序执行会依次弹出’c’,‘d’。

function testC(){

alert('c');

return false;

alert('cc');

}

function testD(){

if(!testC())

return;

alert('d');

}

testD();

两个函数做了修改,testC中返回false,testD中对testC的返回值做了判断,这样终止testC的同时也能将testD终止,程序执行弹出’d’便会终止。