Jump to content

Jquery ajax stop a request if a new one is made?


shortysbest

Recommended Posts

I have an instant search on my website and when you type it searches the database and returns the results, this all works however the problem I have is it returns all of the data, so when you type a number of letters it has a lag and it switches through the results with a delay, sometimes not returning correct results even. I know this is because of the number of requests made while there are requests still being sent to the server, so how would I stop a request, then make a new one?

 

 

function search(string){
$.ajax({
        type: "POST",
        url: "",
        data: "",
        cache: false,
        success: function(html) {

        $("body").html(html);

                                             }
});
 } 

That's what I use now.

 

and this below is my idea of what should happen:

function search(string){
//first stop this request (not all requests being made to the server, due to other queries that go at the same time)

stop_request(search());

//then it continues on with the new request

$.ajax({
        type: "POST",
        url: "",
        data: "",
        cache: false,
        success: function(html) {

        $("body").html(html);
//old request never a replaces data
//new request does.
                                             }
});
}

  • 1 month later...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.