Amin
Amin

Reputation: 741

Re-invoke $.ajax under condition

When I initialize an ajax call, when the request is succeeded and match a condition I want to re-call it again inside it, like following:

$.ajax({
    ...
    success  :  function(){
                    if(true)
                        // run_this_request_again();
                },
    // or,
    complete : function(){
                    if(true)
                        // run_this_request_again();
                },
    ...
});

Upvotes: -3

Views: 34

Answers (2)

Kalimah
Kalimah

Reputation: 11437

You can call this inside success with the ajax method.

$.ajax({
  success: function() {
    if (true)
      $.ajax(this);
  },
  complete: function() {
    if (true)
      $.ajax(this);
  }
});

Upvotes: 1

Gowri
Gowri

Reputation: 1856

Create a function and place you ajax code in it. Call that function wherever you required.

function ajaxCall()
{
    //Your ajax code here
    $.ajax({
        ...
        success:function()
        {
            if(true)
                ajaxCall()
        },
        complete : function()
        {
            if(true)
                ajaxCall()
        },
    });
}

Upvotes: 1

Related Questions