Alegro
Alegro

Reputation: 7956

How to exit from the execution of jquery code?

I have multiple (10) if statemnets stacked in a function, and need something like this:

$len=$("#user").val().length;
   if($len == 0) { do something and then - stop, exit, break ... whatever)

So, if $len is 0 - do something and - dont check anything more. Just exit from the execution.

Upvotes: 0

Views: 7950

Answers (2)

ATOzTOA
ATOzTOA

Reputation: 35980

Just use this...

$len=$("#user").val().length;
if($len == 0) { return false; }

I am returning false to make sure the event which invoked the function won't run anymore.

Upvotes: 1

Adil
Adil

Reputation: 148150

You can use return statement but it will take the execution control at end of function and skip all the statements between. or use if-else block or use switch.

if($len == 0) {

   do something and then - stop, exit, break ... whatever
   return;
)

Use if-else

if($len == 0) {

   do something and then - stop, exit, break ... whatever
   return;
)
else
if($len == 1) {

   do something and then - stop, exit, break ... whatever
   return;
)
//repeate if else

Using switch

switch($len)
{
 case 0:
  //execute code block 1
  break;
case 1:
  //execute code block 1
  break;
case 2:
  //execute code block 2
  break;
default:
  code to be executed if n is different from case 1 and 2
}

Upvotes: 2

Related Questions