Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6801745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:08:15+00:00 2026-05-26T19:08:15+00:00

Possible Duplicate: Kill Ajax requests using JavaScript using jQuery Here is the simple code

  • 0

Possible Duplicate:
Kill Ajax requests using JavaScript using jQuery

Here is the simple code I am working with:

$("#friend_search").keyup(function() {

    if($(this).val().length > 0) {
        obtainFriendlist($(this).val());
    } else {
        obtainFriendlist("");
    }

});

function obtainFriendlist(strseg) {

    $.ajax({
       type: "GET",
       url: "getFriendlist.php",
       data: "search="+strseg,
       success: function(msg){
        UIDisplayFriends(msg);
       }
     });
}

Essentially, if a keyup event is fired before the function obtainFriendlist returns a result (and triggers UIDisplayFriends(msg), I need to cancel the in-flight request. The issue I have been having is that they build up, and then suddenly the function UIDisplayFriends is fired repeatedly.

Thank you very much, and advice is helpful too

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T19:08:16+00:00Added an answer on May 26, 2026 at 7:08 pm

    The return value of $.ajax is an XHR object that you can call actions on. To abort the function you would do something like:

    var xhr = $.ajax(...)
    ...
    xhr.abort()
    

    It may be smart to add some debouncing as well to ease the load on the server. The following will only send an XHR call only after the user has stopped typing for 100ms.

    var delay = 100,
        handle = null;
    
    $("#friend_search").keyup(function() {
        var that = this;
        clearTimeout(handle);
        handle = setTimeout(function() {
            if($(that).val().length > 0) {
                obtainFriendlist($(that).val());
            } else {
                obtainFriendlist("");
            }
        }, delay);
    });
    

    A third thing that you should really be doing is filtering the XHR responses based on whether or not the request is still valid:

    var lastXHR, lastStrseg;
    
    function obtainFriendlist(strseg) {
        // Kill the last XHR request if it still exists.
        lastXHR && lastXHR.abort && lastXHR.abort();
    
        lastStrseg = strseg;
        lastXHR = $.ajax({
           type: "GET",
           url: "getFriendlist.php",
           data: "search="+strseg,
           success: function(msg){
    
            // Only display friends if the search is the last search.
            if(lastStrseg == strseg) 
              UIDisplayFriends(msg);
           }
         });
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: JavaScript: var functionName = function() {} vs function functionName() {} What's the
Possible Duplicate: Cross-browser window resize event - JavaScript / jQuery Jquery flash of unstyled
Possible Duplicate: How do you send email from a Java app using Gmail? How
Possible Duplicate: How to horizontally center a div? One simple way to make an
Possible Duplicate: Resetting a multi-stage form with jQuery Once the form submitted, response from
Possible Duplicate: How to detect if JavaScript is disabled? I have a website which
Possible Duplicate: or is not valid C++ : why does this code compile ?
Possible Duplicate: Parse string into argv/argc I'm trying to write a fake shell using
Possible Duplicate: Can we change the device time using an application? is that possible
Possible Duplicate: Why does “abcd”.StartsWith(“”) return true? Whilst debugging through some code I found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.