คุณสามารถใช้return;ตัวอย่าง
$(document).ready(function () {
alert(1);
return;
alert(2);
alert(3);
alert(4);
});
ผลตอบแทนจะกลับไปที่ฟังก์ชันผู้โทรหลัก test1 (); และดำเนินการต่อจากที่นั่นถึง test3 ();
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<script type="text/javascript">
function test1(){
test2();
test3();
}
function test2(){
alert(2);
return;
test4();
test5();
}
function test3(){
alert(3);
}
function test4(){
alert(4);
}
function test5(){
alert(5);
}
test1();
</script>
</body>
</html>
แต่ถ้าคุณเพิ่มการโยน ''; สิ่งนี้จะหยุดการดำเนินการโดยสมบูรณ์โดยไม่ก่อให้เกิดข้อผิดพลาดใด ๆ
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<script type="text/javascript">
function test1(){
test2();
test3();
}
function test2(){
alert(2);
throw '';
test4();
test5();
}
function test3(){
alert(3);
}
function test4(){
alert(4);
}
function test5(){
alert(5);
}
test1();
</script>
</body>
</html>
ทดสอบกับ firefox และ chrome ฉันไม่รู้ว่า IE หรือ Safari จัดการเรื่องนี้อย่างไร