lodash debounce not working in anonymous function

后端 未结 5 1361
忘了有多久
忘了有多久 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: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);
    

提交回复
热议问题