throttle.js 610 B

12345678910111213141516171819202122
  1. import { debounce } from './debounce'
  2. import { isObject } from './isObject'
  3. let FUNC_ERROR_TEXT = 'Expected a function'
  4. export function throttle(func, wait, options) {
  5. let leading = true,
  6. trailing = true
  7. if (typeof func != 'function') {
  8. throw new TypeError(FUNC_ERROR_TEXT)
  9. }
  10. if (isObject(options)) {
  11. leading = 'leading' in options ? !!options.leading : leading
  12. trailing = 'trailing' in options ? !!options.trailing : trailing
  13. }
  14. return debounce(func, wait, {
  15. 'leading': leading,
  16. 'maxWait': wait,
  17. 'trailing': trailing,
  18. })
  19. }