Advertisement
Guest User

Untitled

a guest
Sep 19th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.56 KB | None | 0 0
  1. /**
  2. * 防抖
  3. * @param {Function} func 执行的函数
  4. * @param {Number} wait 防抖的时间
  5. * @param {Boolean} immediate 是否为立即执行
  6. */
  7. function debounce(func, wait, immediate) {
  8. let time = null;
  9. return function() {
  10. let _this = this;
  11. let args = arguments;
  12. if (time) clearTimeout(time);
  13. if (immediate) {
  14. let callNow = !time;
  15. setTimeout(() => {
  16. time = null;
  17. }, wait);
  18. if (callNow) func.apply(_this, args);
  19. } else {
  20. time = setTimeout(() => {
  21. func.apply(_this, args);
  22. }, wait);
  23. }
  24. };
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement