Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

time an ajax request

Posted on 2012-04-11
3
458 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
ID: 37834311
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
ID: 37836738
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
ID: 37837476
Ok. That is a really good idea of doing it. I will try that out and post back. Thanks!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to deal with the query string 8 37
Html CheckBox obtain Its Value 5 27
Asp in script 6 35
JS Event Does not Trigger From File 2 23
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)

792 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