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 6134241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:19:48+00:00 2026-05-23T17:19:48+00:00

This is my Ajax: $(form[0] :text).live(keyup, function(event) { event.preventDefault(); $(‘.result’).remove(); var serchval = $(form[0]

  • 0

This is my Ajax:

$("form[0] :text").live("keyup", function(event) {

    event.preventDefault();
    $('.result').remove();
    var serchval = $("form[0] :text").val();

    if(serchval){

        $.ajax({

            type: "POST",
            url: "<?= site_url('pages/ajax_search') ?>",
            data: {company : serchval},
            success: function(data) {

                var results = (JSON.parse(data));
                console.log(results);

                if(results[0]){
                    $.each(results, function(index) {
                        console.log(results[index].name);
                        $("#sresults").append("<div class='result'>" + results[index].name + "</div>");
                    });
                }
                else {
                    $("#sresults").append("<div class='result'>לא נמצאו חברות</div>");
                }
            }
        });
    }
});

When I type slowly (slower then a letter per second) I get the results correct, when I type faster I get 2 times the same results

example:
slow typing: res1 res2 res3
fast typing: res1 res2 res3 res1 res2 res3

Also, any advice on improving the code would be welcome!

  • 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-23T17:19:49+00:00Added an answer on May 23, 2026 at 5:19 pm

    Thats what is happening (pseudocode):

    When you’re typing slow:

    .keyup1
    .remove1
    //asynchronous ajax1 request takes some time here...
    .append1
    .keyup2
    .remove2
    //asynchronous ajax2 request takes some time here...
    .append2
    

    When you’re typing fast:

    .keyup1
    .remove1
    //asynchronous ajax1 request takes some time here...
    //and keyup2 happens before ajax1 is complete
    .keyup2
    .remove2
    .append1
    //asynchronous ajax2 request takes some time here...
    .append2
    //two results were appended _in a row_ - therefore duplicates
    

    To solve duplicates problem, you would want to make your results removing/appending an atomic operation – using .replaceWith.

    Build results HTML block first as string and then do the .replaceWith instead of .remove/.append:

    var result = '';
    for (i in results) {
        result += "<div class='result'>" + results[i].name + "</div>";
    }
    
    $("#sresults").replaceWith('<div id="sresults">' + result + '</div>');
    

    Another problem (not related to duplicates) may be that older result overwrites newer which arrived earlier (because AJAX is asynchronous and server may issue responses not in the same order it receives requests).

    One approach to avoid this is attaching roundtrip marker (kind of “serial number”) to each request, and checking it in response:

    //this is global counter, you should initialize it on page load, global scope
    //it contains latest request "serial number"
    var latestRequestNumber = 0;
    
    $.ajax({
        type: "POST",
        url: "<?= site_url('pages/ajax_search') ?>",
        //now we're incrementing latestRequestNumber and sending it along with request
        data: {company : serchval, requestNumber: ++latestRequestNumber},
        success: function(data) {
            var results = (JSON.parse(data));
            //server should've put "serial number" from our request to the response (see PHP example below)
            //if response is not latest (i.e. other requests were issued already) - drop it
            if (results.requestNumber < latestRequestNumber) return;
            // ... otherwise, display results from this response ...
        }
    });
    

    On server side:

    function ajax_search() {
        $response = array();
    
        //... fill your response with searh results here ...
    
        //and copy request "serial number" into it
        $response['requestNumber'] = $_REQUEST['requestNumber'];
    
        echo json_encode($response);
    }
    

    Another approach would be to make .ajax() requests synchronous, setting async option to false. However this may temporarily lock the browser while request is active (see docs)

    And also you should definitely introduce timeout as algiecas suggests to reduce load on server (this is third issue, not related to duplicates nor to request/response order).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm doing a jQuery AJAX post like this: var form = $(#formid); form.submit(function() {
I have a page like this: <form class=ajax method=post action=add_item.php> [text input] [submit button]
I have an ajax form in asp.net mvc which is as simple as this:
On document ready I run this code: jQuery(document).ready(function(){ jQuery('#button').click(function() { jQuery('#contact_form').load(/Users/mge/Downloads/jquery-ajax-1/readme.txt); return false; });
Could someone tell me if this: $.ajax({ url: 'test.html', success: function(data) { alert(Data Loaded:
I am working on this website which requires a jquery/ajax contact form. Everything works
jQuery $('#speichern').live('click' , function () { // [a] var data_save = $('#form_rechn').serializeArray(); var data_save_ser
I have this bit of JavaScript... 15 $('.ajax_edit_address').each(function() { 16 $(this).ajaxForm({ 17 target: $(this).parents('table.address').find('tr.address_header').children(':first'),
I have this AJAX code, but it doesn't seem to throw the 'alert' method.
How do you get around this Ajax cross site scripting problem on FireFox 3?

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.