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.
                                             }
});
}

Link to comment
Share on other sites

  • 1 month later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.