$ npm install @f/throttle
Throttling a function causes that function to only execute, at most, once every
ms milliseconds. Each time it's called, one of two things happens:
- If there is no pending run, one is queued to run in
- If one is pending, nothing happens. It just continues to wait for the pending one to run.
var throttle = require('@f/throttle') // Runs at most every 100 milliseconds, regardless of how many scroll events happen window.addEventListener('scroll', throttle(updateScrollSpy, 100))
fn- The function to throttle
ms- The time in milliseconds to throttle it for.
Returns: A throttled version of
fn. When called, this function returns a
cancel function that can be used to stop a pending execution.