Dynamic Jquery table constructed using ajax response data on textbox's each keyup event, not showing data of last ajax response (last keyup)

user2918640

I am using ajax to fetch row data for a table based on the input entered in a textbox.

In my case, pincode input '1000' fetches 10 location rows and input '10007' fetches one row.

Following is the js code

 $('#pincode').keyup(function(event){
    var currentElementId = $(this).attr('id');
    enterlocation = document.getElementById(currentElementId).value;
    var len = enterlocation.length;

    if (len != null && len > 2 && event.which != 8) {
    var lastRequestTimeStamp = new Date().getTime();
        $.ajax({
            type : "GET",
            url : "/abc.htm",
            cache : false,
            contentType : "application/json;charset=utf-8",
            dataType : 'json',
            data : {
                searchKey : enterlocation, lastRequestTimeStampSent : lastRequestTimeStamp},
            beforeSend : function() {
                $("#mytable").remove();
            },
            success : function(response) {
            var locations = response;
            var sentTimeStamp = parseInt(lastRequestTimeStamp, 10);
            var recievedTimestamp = parseInt(locations[0].requestTimeStamp, 10);
                $("#mytable").show();
                var locations = response;

                if (currentElementId == "pincode") {
                    $("#div1").append('<table id="mytable"></table>');
                }
                if (currentElementId == "city") {
                    $("#div2").append('<table id="mytable"></table>');
                }
                if (currentElementId == "street") {
                    $("#div3").append('<table id="mytable"></table>');
                }


                if ((sentTimeStamp == recievedTimestamp) && locations != null && locations.length > 0) {
                    $.each(locations, function(index, location) {
                        var locationRow = '<tr id="locationRowId'+index+'">' +
                        '<td>' +    
                        '<input type= "hidden" id ="hSugAddrLine1'+index+'" name ="hSugAddrLine1'+index+'" value="'+replaceNullwithBlank(location.addressLine1)+'">' +
                        '</td></tr>';

                        if ($("#mytable").children().length == 0) {
                            $("#mytable").append(locationRow);
                        } else {
                            $("#mytable").find('tbody:last').append(locationRow);
                        }
                    });
                }
            },
            error : function(jqXHR, textStatus, errorThrown) {
                alert("@Error");
                console.log(textStatus, errorThrown);
            },
            complete : function() {
                $("#mytable").show();
                //$("#mytable").table("refresh");
            }
        });
    }

});

As I enter 10007 swiftly in the input box, what I get in the table is the response data for the pincode '1000' in most cases or response data for pincode '100' in some cases.

But when I enter '10007' slowly in the input text box, I get the correct ajax response data in the table as meant for input '10007'.

It appears to me some slow rendering issue. As mentioned, input '1000' response data is larger than that of '10007', so I think previous ajax call larger data is rendering on table even after next ajax call response is rendering. Being new and naive for jquery and ajax, I am quite brainstorming here. Help please!

user1932079

You're calling your server too fast... every keyup. That will cause problems with any kind of heavy use. What you may want to do is set a timer when a keyup event occurs, then when the timer is called, execute the AJAX call. Set the timer for something like 1 second so that after a key has not been pressed for 1 second, the call is made. This will reduce the extra calls.

Additionally, it wouldn't hurt to assign a timestamp id to the ajax call which is returned by the server with the results so that you can use only the last-sent AJAX call in populating your results.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

jQuery on 'keyup' no longer working after AJAX request

From Dev

Displaying data from an Ajax response with Jquery

From Dev

jQuery textbox validation using both on keyup and blur

From Dev

Jquery ajax populate dropdown with json response data

From Dev

jquery keyup function: enabling/disabling a textbox using keyup()

From Dev

send data to php with jquery AJAX and retrieve the response

From Dev

Adding a delay to JQuery keyup() after Ajax call

From Dev

Each request of ajax give the data in response

From Dev

for each data jquery ajax

From Dev

How to set AJAX response to jquery data table columns?

From Dev

jQuery .each function runs over and ajax response showing later

From Dev

jQuery: keyup event - ajax return validation

From Dev

Jquery ajax populate dropdown with json response data

From Dev

Cannot Bind JSON response to textbox using jquery ajax (MVC 4)

From Dev

jQuery: dialog() of table data from ajax not showing

From Dev

insert ajax response in data table

From Dev

Ajax response not showing in JS

From Dev

How to re-create jQuery data-table on ajax response?

From Dev

Ajax Data response data not alert

From Dev

Using jQuery to build table rows from Ajax response (not with static json data)

From Dev

AJAX $.post not showing response data

From Dev

jQuery: Post data from ajax response

From Dev

for each data jquery ajax

From Dev

Unable to access AJAX Response Data through jQuery

From Dev

jQuery Ajax response 200, but cannot access the response data

From Dev

Ajax response undefined on .keyup operation

From Dev

How to show json response data in datatable using jQuery ajax request?

From Dev

Compare data with ajax response using jquery

From Dev

Jquery + Ajax, Params has data but response says no data

Related Related

  1. 1

    jQuery on 'keyup' no longer working after AJAX request

  2. 2

    Displaying data from an Ajax response with Jquery

  3. 3

    jQuery textbox validation using both on keyup and blur

  4. 4

    Jquery ajax populate dropdown with json response data

  5. 5

    jquery keyup function: enabling/disabling a textbox using keyup()

  6. 6

    send data to php with jquery AJAX and retrieve the response

  7. 7

    Adding a delay to JQuery keyup() after Ajax call

  8. 8

    Each request of ajax give the data in response

  9. 9

    for each data jquery ajax

  10. 10

    How to set AJAX response to jquery data table columns?

  11. 11

    jQuery .each function runs over and ajax response showing later

  12. 12

    jQuery: keyup event - ajax return validation

  13. 13

    Jquery ajax populate dropdown with json response data

  14. 14

    Cannot Bind JSON response to textbox using jquery ajax (MVC 4)

  15. 15

    jQuery: dialog() of table data from ajax not showing

  16. 16

    insert ajax response in data table

  17. 17

    Ajax response not showing in JS

  18. 18

    How to re-create jQuery data-table on ajax response?

  19. 19

    Ajax Data response data not alert

  20. 20

    Using jQuery to build table rows from Ajax response (not with static json data)

  21. 21

    AJAX $.post not showing response data

  22. 22

    jQuery: Post data from ajax response

  23. 23

    for each data jquery ajax

  24. 24

    Unable to access AJAX Response Data through jQuery

  25. 25

    jQuery Ajax response 200, but cannot access the response data

  26. 26

    Ajax response undefined on .keyup operation

  27. 27

    How to show json response data in datatable using jQuery ajax request?

  28. 28

    Compare data with ajax response using jquery

  29. 29

    Jquery + Ajax, Params has data but response says no data

HotTag

Archive