OpenC3/cosmos

View on GitHub
openc3-cosmos-init/plugins/packages/openc3-cosmos-ace-diff/src/helpers/throttle.js

Summary

Maintainability
A
0 mins
Test Coverage
export default function throttle(callback, wait, immediate = false) {
  let timeout = null
  let initialCall = true

  return (...args) => {
    const callNow = immediate && initialCall
    const next = () => {
      callback.apply(this, args)
      timeout = null
    }

    if (callNow) {
      initialCall = false
      next()
    }

    if (!timeout) {
      timeout = setTimeout(next, wait)
    }
  }
}