JS/JQuery Search Box Causing lag

user8023239 Source

I would like to Thank You for taking a look!

However my issue is that my search box causes a lot of lag while it is searching, This is because of the feature it has of Searching While Still Typing, it is searching through approx 300-350 items. I cannot seem to figure out how to solve this issue I am looking for some guidance or a solution please!

        <div class="col-md-3 pull-right">
            <form action="#" method="get">
                <div class="input-group">
                    <input class="form-control" id="system-search" name="q" placeholder="Find a Player.." required>
                    <span class="input-group-btn">
                        <button type="submit" class="btn btn-default"><i class="glyphicon glyphicon-search" style="padding-top: 3px;padding-bottom: 3px;background-color:rgb(33, 154, 83);color:white;
    text-shadow: 0 1px 0 rgb(33, 154, 83);"></i></button>
                    </span>
                </div>
            </form>
        </div>

<table id='agent_commission_model' class="table table-list-search" style="margin-top:30px;">

$(document).ready(function() {
    var activeSystemClass = $('.list-group-item.active');

    //something is entered in search form
    $('#system-search').keyup( function() {
       var that = this;
        // affect all table rows on in systems table
        var tableBody = $('.table-list-search tbody');
        var tableRowsClass = $('.table-list-search tbody tr');
        $('.search-sf').remove();
        tableRowsClass.each( function(i, val) {
        
            //Lower text for case insensitive
            var rowText = $(val).text().toLowerCase();
            var inputText = $(that).val().toLowerCase();
            if(inputText != '')
            {
                $('.search-query-sf').remove();
                tableBody.prepend('<tr class="search-query-sf"><td colspan="6"><strong>Searching for: "'
                    + $(that).val()
                    + '"</strong></td></tr>');
            }
            else
            {
                $('.search-query-sf').remove();
            }

            if( rowText.indexOf( inputText ) == -1 )
            {
                //hide rows
                tableRowsClass.eq(i).hide();
                
            }
            else
            {
                $('.search-sf').remove();
                tableRowsClass.eq(i).show();
            }
        });
        //all tr elements are hidden
        if(tableRowsClass.children(':visible').length == 0)
        {
            tableBody.append('<tr class="search-sf"><td class="text-muted" colspan="6">No entries found.</td></tr>');
        }
    });
});

(Also I wasn't too sure how to label this, or title it pardon me if it's wrong)

javascriptjquery

Answers

answered 3 months ago skobaljic #1

People may type several characters per second, so you should delay the search this way:

var delay=null;
//something is entered in search form
$('#system-search').keyup( function() {
    clearTimeout( delay );
    delay = setTimeout( yourSearchFunction, 300);
)};

Also, appending rows one by one is not a good solution, better gather all the new dom elements and append them at once.

Also, inside the loop:

 tableRowsClass.each( function(i, val) {

using

 tableRowsClass.eq(i)

has no much sense, should be:

 tableRowsClass.each( function(i, row) {
      var thisRow = $(row);
      /* than for example use */
      thisRow.hide();

I would suggest you start over and simplify the dom manipulation.

comments powered by Disqus