function throttle (func, wait, options = {}) {
  let timeout, context, args, result
  let previous = 0

  const later = function () {
    previous = options.leading === false ? 0 : now()
    timeout = null
    result = func.apply(context, args)
    if (!timeout) context = args = null
  }

  const throttled = function () {
    let at = now()
    if (!previous && options.leading === false) previous = at
    let remaining = wait - (at - previous)
    context = this
    args = arguments
    if (remaining <= 0 || remaining > wait) {
      if (timeout) {
        clearTimeout(timeout)
        timeout = null
      }
      previous = at
      result = func.apply(context, args)
      if (!timeout) context = args = null
    } else if (!timeout && options.trailing !== false) {
      timeout = setTimeout(later, remaining)
    }
    return result
  }

  throttled.cancel = function () {
    clearTimeout(timeout)
    previous = 0
    timeout = context = args = null
  }

  return throttled
}
Returns a function, that, when invoked, will only be triggered
at most once during a given window of time.

@param {Function} func
@param {Number} wait
@param {Object=} options
@return {Function}

@see https://github.com/jashkenas/underscore