debounce.js 338 B

1234567891011121314151617
  1. /**
  2. * 防抖
  3. * @param {Object} scope //引用的this,发现不显式传this,拿不到。
  4. * @param {Object} fn
  5. * @param {Object} delay
  6. */
  7. let t = null
  8. const debounce = function(scope, fn, delay) {
  9. if (t !== null) {
  10. clearTimeout(t)
  11. }
  12. t = setTimeout(() => {
  13. scope[fn]()
  14. }, delay)
  15. }
  16. export default debounce