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
41 lines (34 loc) · 803 Bytes
/
debounce.js
File metadata and controls
41 lines (34 loc) · 803 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
40
41
let now = Date.now
export default function (fn, wait = 250, immediate) {
let
timeout, params, context, timestamp, result,
later = () => {
let last = now() - timestamp
if (last < wait && last >= 0) {
timeout = setTimeout(later, wait - last)
}
else {
timeout = null
if (!immediate) {
result = fn.apply(context, params)
if (!timeout) {
context = params = null
}
}
}
}
return function (...args) {
var callNow = immediate && !timeout
context = this
timestamp = now()
params = args
if (!timeout) {
timeout = setTimeout(later, wait)
}
if (callNow) {
result = fn.apply(context, args)
context = params = null
}
return result
}
}