ministryofjustice/Claim-for-Crown-Court-Defence

View on GitHub
app/webpack/javascripts/modules/Modules.Debounce.js

Summary

Maintainability
A
25 mins
Test Coverage
moj.Modules.Debounce = {

  init: function (func, wait, immediate) {
    let timeout
    return function executedFunction () {
      const context = this
      const args = arguments

      const later = function () {
        timeout = null
        if (!immediate) func.apply(context, args)
      }
      const callNow = immediate && !timeout
      clearTimeout(timeout)

      timeout = setTimeout(later, wait)
      if (callNow) func.apply(context, args)
    }
  }
}