$.fn.dataTable.util.throttle not throttling search (server-side)

$.fn.dataTable.util.throttle not throttling search (server-side)

jrizzi1jrizzi1 Posts: 44Questions: 12Answers: 3

Here is my example

In my example, I have throttling set at 2500, which should throttle requests to 1 every 2.5 seconds, however I am seeing no throttle occurring, it is sending a request every keyup , same as before I included the throttle method

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,687Questions: 1Answers: 10,500 Site admin
    Answer ✓

    Thanks for moving this over from GitHub. In your example you use the throttle utility function to create a throttled function assigned to the variable search:

    var search = $.fn.dataTable.util.throttle(
        function ( val ) {
            $table.search( val ).draw();
        },
        25000
    );
    

    Which is fine, but you never use it. Instead the code goes on to do:

    $table.columns().eq( 0 ).each( function ( colIdx ) {
        $( 'input', $table.column( colIdx ).footer() ).on( 'keyup change', function () {
            $table
                .column( colIdx )
                .search( this.value )
                .draw();
        } );
    } );
    

    which does exactly what it is told to do - perform a search on every keyup and change event.

    What you need to do is wrap that inner function in a throttle and use that in the event handler. Something like:

    $table.columns().eq( 0 ).each( function ( colIdx ) {
        var throttledSearch = $.fn.dataTable.util.throttle(
            function () {
                $table
                    .column( colIdx )
                    .search( this.value )
                    .draw();
            },
            2500
        );
    
        $( 'input', $table.column( colIdx ).footer() ).on( 'keyup change', throttledSearch );
    } );
    

    http://live.datatables.net/laxinabe/5/edit

    Note that there is a bug in DataTables at the moment whereby the throttled function will execute immediately and then delay kicks in. That is something I will address in the next release.

    Allan

  • jrizzi1jrizzi1 Posts: 44Questions: 12Answers: 3

    thanks Allan, much appreciated

  • mtbmtbmtbmtb Posts: 3Questions: 1Answers: 0

    Hi Allan,

    Was wondering if there is any update on this issue.

    Thanks

  • allanallan Posts: 63,687Questions: 1Answers: 10,500 Site admin

    Regarding my latest statement for the above post? Sorry, haven't had a chance to look at it yet with everything else going on.

    Allan

This discussion has been closed.