Basic usage

Throttle accepts an options object with an interval and a source function to call when invoked. When the returned function is invoked it will only call the source function if the interval milliseconds of time has passed. Otherwise, it will ignore the invocation.

import { throttle } from 'radash'

const onMouseMove = () => {
  rerender()
}

addEventListener('mousemove', throttle({ interval: 200 }, onMouseMove))

Timming

A visual of the throttle behavior when interval is 200. The throttle function returned by throttle can be called every millisecond but it will only call the given callback after interval milliseconds have passed.

                Time: 0ms - - - - 100ms - - - - 200ms - - - - 300ms - - - - 400ms - - - - 
Throttle Invocations: x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x - - -
  Source Invocations: x - - - - - - - - - - - - x - - - - - - - - - - - - - x - - - - - -

Testing

This _.throttle function is 100% covered by tests. See them and contribute at GitHub.

Source

See the source for _.throttle on GitHub.