lodash debounce not working in anonymous function

后端 未结 5 1350
忘了有多久
忘了有多久 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

    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));
    

提交回复
热议问题