Pararera
Pararera

Reputation: 372

How to stop jQuery code

I want to stop jQuery code if is response equal to something. I tried with return false and return true, but that way doesn't work. Ajax code is on top in $(document).ready(function(). Problem is because I have another ajax code below this code, and that ajax code shouldn't run if response in frist ajax isn't 1, 2, 3, or 4.

$.ajax({
    type: "POST",
    url: "_hsync_scripts/_hsync_provjeri_reg.php",
    success: function(response)
    {       
        if(Number(response) != 1 && Number(response) != 2 && Number(response) != 3 && Number(response) != 4)
        {
            $('#_hsync_reg_ugasena').modal("show");
            return false;
        }
    }
});

Upvotes: 0

Views: 45

Answers (4)

Mukesh Ram
Mukesh Ram

Reputation: 6328

You can use some conditional tags, define any variable before ajax and update that if frist ajax isn't 1, 2, 3, or 4.so now you code like.

var data = '';
$.ajax({
type: "POST",
url: "_hsync_scripts/_hsync_provjeri_reg.php",
success: function(response)
{       
    if(Number(response) != 1 && Number(response) != 2 && Number(response) != 3 && Number(response) != 4)
    {
        $('#_hsync_reg_ugasena').modal("show");
        data = 1;
        return false;
    }
}

});

Now use this data variable for your next ajax call like.

if(data != 1){
   $.ajax({

          Your code;
    });
}

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

Put the other AJAX call inside the success function:

$.ajax({
    type: "POST",
    url: "_hsync_scripts/_hsync_provjeri_reg.php",
    success: function(response) {       
        if(Number(response) != 1 && Number(response) != 2 && Number(response) != 3 && Number(response) != 4) {
            $('#_hsync_reg_ugasena').modal("show");
        } else {
            // Do your other AJAX call here
            $.ajax({...});
        }
    }
});

Upvotes: 1

Turnip
Turnip

Reputation: 36632

Call your second function from the success callback of the first only if your condition is met:

$.ajax({
    type: "POST",
    url: "_hsync_scripts/_hsync_provjeri_reg.php",
    success: function(response)
    {       
        if(Number(response) = 1 && Number(response) != 2 && Number(response) != 3 && Number(response) != 4)
        {
            $('#_hsync_reg_ugasena').modal("show");
            return false; // not needed
        } else {

            mySecondAjax();

        }
    }
});

Upvotes: 1

CY_
CY_

Reputation: 7608

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

Upvotes: 0

Related Questions