[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

time an ajax request

Posted on 2012-04-11
3
Medium Priority
?
469 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
[X]
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
3 Comments
 
LVL 20

Assisted Solution

by:BuggyCoder
BuggyCoder earned 1000 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 1000 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

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…
How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
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)

649 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