0

I have a search capability on my webpage where it searches for something on my DB using Ajax and returns results placing them on a modal box on the success function. After they are loaded it shows the modal. I am using django as a backend. Is there a way to stop the ajax call or maybe to know when the response is taking to long to happen? d

Apostolos
  • 7,763
  • 17
  • 80
  • 150

2 Answers2

1
$.ajax({
    url: "test.html",
    error: function(){
        // will fire when timeout is reached
    },
    success: function(){
        //do something
    },
    timeout: 3000 // sets timeout to 3 seconds
});

From: Set timeout for ajax (jQuery)

Community
  • 1
  • 1
Aboca
  • 575
  • 2
  • 9
  • Thank you! Which other cases does "error" include? 500 SERVER Error? I think I have to check the status code on success if I want to catch the 500 correct? – Apostolos May 29 '14 at 13:44
  • 500 server error is tracted as error, to get it's error code catch it from the response. error: function(response){ if(response.status == 500){} } – Aboca May 29 '14 at 14:01
0

Here is how to do it in jQuery

   $.ajax({
     type: "POST",
     url: "some.php",
     data: { name: "John", location: "Boston" }
     timeout: 4000 /*Miliseconds*/
   })
     .done(function( msg ) {
       alert( "Data Saved: " + msg );
     });

Vinilla JS i much more complex but the code you would end up with would be something like this

   xhr = new XMLHttpRequest();
   // ALOT OF OTHER CODE....
   xhr.timeout = 4000;
James Harrington
  • 3,138
  • 30
  • 32