forked from quasarframework/quasar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebounce.js
More file actions
39 lines (31 loc) · 665 Bytes
/
debounce.js
File metadata and controls
39 lines (31 loc) · 665 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
export function debounce (fn, wait = 250, immediate) {
let timeout
function debounced (...args) {
const later = () => {
timeout = null
if (!immediate) {
fn.apply(this, args)
}
}
clearTimeout(timeout)
if (immediate && !timeout) {
fn.apply(this, args)
}
timeout = setTimeout(later, wait)
}
debounced.cancel = () => {
clearTimeout(timeout)
}
return debounced
}
export function frameDebounce (fn) {
let wait = false
return function (...args) {
if (wait) { return }
wait = true
window.requestAnimationFrame(() => {
fn.apply(this, args)
wait = false
})
}
}