maestro-server/bootue

View on GitHub
src/_core/_utils/delayer.js

Summary

Maintainability
A
0 mins
Test Coverage

export default function delayer (fn, varTimer, ifNaN = 100) {
  function toInt (el) {
    return /^[0-9]+$/.test(el) ? Number(el) || 1 : null
  }

  let timerId
  return function (...args) {
    if (timerId) clearTimeout(timerId)
    timerId = setTimeout(() => {
      fn.apply(this, args)
    }, toInt(varTimer) || toInt(this[varTimer]) || ifNaN)
  }
}