Apostolos
Apostolos

Reputation: 8111

Stop an ajax call if it takes to much to complete

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

Upvotes: 0

Views: 392

Answers (2)

James Harrington
James Harrington

Reputation: 3226

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;

Upvotes: 0

Aboca
Aboca

Reputation: 575

$.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)

Upvotes: 1

Related Questions