Radash
  1. Curry
  2. throttle

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

Timing

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

isThrottled

The function returned by throttle has a isThrottled method that when called will return if there is any active throttle.

const debounced = throttle({ interval: 200 }, onMouseMove)

// ... sometime later

debounced.isThrottled()