debounce.js 580 B

1234567891011121314151617181920212223242526
  1. /**
  2. * @desc 防抖函数
  3. * @param {Function} callBack
  4. * @return {Function} fn
  5. */
  6. var debounce = function debounce(fn) {
  7. var delay = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 500;
  8. var timer = void 0;
  9. return function () {
  10. var _this = this;
  11. for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
  12. args[_key] = arguments[_key];
  13. }
  14. if (timer) {
  15. clearTimeout(timer);
  16. }
  17. timer = setTimeout(function () {
  18. fn.apply(_this, args);
  19. }, delay);
  20. };
  21. };
  22. module.exports = debounce;