lodash debounce not working in anonymous function

后端 未结 5 1340
忘了有多久
忘了有多久 2020-12-08 18:08

Hello I cannot seem to figure out why the debounce function works as expected when passed directly to a keyup event; but it does not work if I wrap it inside an anonymous fu

相关标签:
5条回答
  • 2020-12-08 18:35

    You can return the debounce function like this:

    (function(){
        var debounce = _.debounce(fireServerEvent, 500, false);
    
        $('#anonFunction').on('keyup', function () {
            //clear textfield
            $('#output').append('clearNotifications<br/>');
            return debounce();
        });
    
        function fireServerEvent(){
            $('#output').append('serverEvent<br/>');
        }
    })();
    
    0 讨论(0)
  • 2020-12-08 18:35

    More generally, if you want a debounce with a trailing behaviour (accounts for last click, or more likely last change on a select input), and a visual feedback on first click/change, you are faced with the same issue.

    This does not work:

    $(document).on('change', "#select", function() {
        $('.ajax-loader').show();
        _.debounce(processSelectChange, 1000);
    });
    

    This would be a solution:

    $(document).on('change', "#select", function() {
        $('.ajax-loader').show();
    });
    $(document).on('change', "#select", _.debounce(processSelectChange, 1000));
    
    0 讨论(0)
  • 2020-12-08 18:41

    Think easier

    _.debounce returns a debounced function! So instead of thinking in terms of

    $el.on('keyup'), function(){
       _.debounce(doYourThing,500); //uh I want to debounce this
    }
    

    you rather call the debounced function instead

    var doYourThingDebounced = _.debounce(doYourThing, 500); //YES, this will always be debounced
    
    $el.on('keyup', doYourThingDebounced);
    
    0 讨论(0)
  • 2020-12-08 18:47

    debounce doesn't execute the function, it returns a function with the debounciness built into it.

    Returns

    (Function): Returns the new debounced function.

    So your #function handler is actually doing the Right Thing, by returning a function to be used by jQuery as a keyup handler. To fix your #noReturnAnonFunction example, you could simply execute the debounced function in the context of your function:

    $('#noReturnAnonFunction').on('keyup', function () {
        _.debounce(debounceIt, 500, false)(); // Immediately executes
    });
    

    But that's introducing a needless anonymous function wrapper around your debounce.

    0 讨论(0)
  • 2020-12-08 18:54

    As Palpatim explained, the reason lies in the fact that _.debouce(...) returns a function, which when invoked does its magic.

    Therefore in your #anonFunction example, you have a key listener, which when invoked does nothing but return a function to the invoker, which does nothing with the return values from the event listener.

    This is a snippet of the _.debounce(...) definition:

    _.debounce
    function (func, wait, immediate) {
        var timeout;
        return function() {
          var context = this, args = arguments;
          var later = function() {
            timeout = null;
            if (!immediate) func.apply(context, args);
          };
          if (immediate && !timeout) func.apply(context, args);
          clearTimeout(timeout);
          timeout = setTimeout(later, wait);
        };
      } 
    

    Your key event listener must invoke the returned function from _.debounce(...), or you can do as in your non-anonymous example and use the returned function from the _.debounce(...) call as your event listener.

    0 讨论(0)
提交回复
热议问题