Solved

time an ajax request

Posted on 2012-04-11
3
452 Views
Last Modified: 2012-04-25
Is there any way to time how long a jquery ajax request has been going on? sometimes searches take too long and it would be nice to add a jquery abort() button if the search takes over, say, 5 seconds. Any way I can do this!

On the other end of the ajax request is a php file that makes a postgresql request.

Much thanks for any ideas!
0
Comment
Question by:hibbsusan
3 Comments
 
LVL 20

Assisted Solution

by:BuggyCoder
BuggyCoder earned 250 total points
Comment Utility
here is how you can timeout ajax request if it doesn't respond in stipulated time period:-

$.ajax({
    url: "yourphpurl",
    type: "GET",
    dataType: "json",
    timeout: 5000, //5 secs time out
    success: function(response) { alert(response); },
    error: function(x, t, m) {
        if(t==="timeout") {
            alert("timed out");
        } else {
            alert(t);
        }
    }
});¿

Open in new window

0
 
LVL 23

Accepted Solution

by:
basicinstinct earned 250 total points
Comment Utility
The returned object from $.ajax is an XMLHttpRequest object. This object has an "abort" method. Call this method to cancel the request.

So you could do something like this:

(function(){
  var xhr;
  $('#foo').bind('click', function() {
    if(xhr)
    {
      xhr.abort();
      xhr = null;
      $(this).text("Search");
    }
    else
    {
        $(this).text("Cancel");
        xhr = $.ajax({blah blah blah, your request params...});
    }
  });
})();
0
 

Author Comment

by:hibbsusan
Comment Utility
Ok. That is a really good idea of doing it. I will try that out and post back. Thanks!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

OverviewThis article demonstrates a simple search form using AJAX. The purpose of the article is to demonstrate how to use the same code to render a page and javascript (JQuery) and AJAX to make subsequent calls to refine the results. The princip…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now