Finn
Finn

Reputation: 1459

How to set timeout for JQuery $.ajax synchronous request and to do something

if I have a synchronous communication ajax ($.ajax function), how to set a timeout for the ajax.

When the countdown ends, via function or any way to return false.

I want to achieve this approach:

ex:

if $.ajax is not timeout 
        => call function A() or do something; 

 else if $.ajax is timeout 
      => call function B() or do something; 

maybe, detection the working time through other function,ex: javascript setInterval();,setTimeout(); ?

Upvotes: 0

Views: 553

Answers (1)

L.A.
L.A.

Reputation: 147

you can try this:

$.ajax({
  url: "url",
  error: function(){
   //error code
  },
  success: function(){
   //success code
  },
  timeout: 3000 //ajax timeout
});

Upvotes: 1

Related Questions