1234567891011121314151617181920212223242526272829 |
- let timeout = null
- function debounce(func, wait = 500, immediate = false) {
-
- if (timeout !== null) clearTimeout(timeout)
-
- if (immediate) {
- const callNow = !timeout
- timeout = setTimeout(() => {
- timeout = null
- }, wait)
- if (callNow) typeof func === 'function' && func()
- } else {
-
- timeout = setTimeout(() => {
- typeof func === 'function' && func()
- }, wait)
- }
- }
- export default debounce
|