Lodash 油门 - 防止函数在延迟后被调用额外的时间

Gal*_*van 6 javascript lodash

我想使用 lodash 油门使函数每 4 秒左右可调用一次不超过一次。

如果用户尝试连续多次激活该功能,则只有第一次点击才能启动该功能。然而,该函数在延迟后被调用额外的时间。立即调用一次,延迟后再次调用。

如何防止额外呼叫?

thing = _.throttle(function() {
  console.log('function runs');
}, 4000);
Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>

<button onclick="thing()">click a few times</button>
Run Code Online (Sandbox Code Playgroud)

小提琴

enn*_*oid 7

thing =  _.throttle( function() {

   $('#info').append('function runs' + '<br />') 

}, 4000, {trailing:false});
Run Code Online (Sandbox Code Playgroud)

https://lodash.com/docs/#throttle