Skip to content

Commit db9a91f

Browse files
committed
feat: ability to cancel frameDebounce()
1 parent a127c68 commit db9a91f

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

src/utils/debounce.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
export function debounce (fn, wait = 250, immediate) {
32
let timeout
43

@@ -25,15 +24,22 @@ export function debounce (fn, wait = 250, immediate) {
2524
}
2625

2726
export function frameDebounce (fn) {
28-
let wait = false
27+
let wait = false, frame
2928

30-
return function (...args) {
29+
function debounced (...args) {
3130
if (wait) { return }
3231

3332
wait = true
34-
window.requestAnimationFrame(() => {
33+
frame = window.requestAnimationFrame(() => {
3534
fn.apply(this, args)
3635
wait = false
3736
})
3837
}
38+
39+
debounced.cancel = () => {
40+
window.cancelAnimationFrame(frame)
41+
wait = false
42+
}
43+
44+
return debounced
3945
}

0 commit comments

Comments
 (0)