-
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy paththrottle.js
More file actions
24 lines (20 loc) · 577 Bytes
/
Copy paththrottle.js
File metadata and controls
24 lines (20 loc) · 577 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
module.exports = function (func, delay) {
let timeoutId
return function () {
const context = this
const args = arguments
// If there is a scheduled execution, cancel it
if (timeoutId) {
clearTimeout(timeoutId)
}
// Immediately execute the function for the first invocation
if (!timeoutId) {
func.apply(context, args)
}
// Schedule the next execution after the specified delay
timeoutId = setTimeout(() => {
timeoutId = null // Reset timeoutId after the delay
func.apply(context, args)
}, delay)
}
}