-
-
Notifications
You must be signed in to change notification settings - Fork 233
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2cfe5dc
commit c9b52ca
Showing
11 changed files
with
159 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ module.exports = throttle; | |
* Creates a throttled function that limits the rate of execution of the provided function. | ||
* The throttled function ensures that the wrapped function is not invoked more frequently | ||
* than a specified time interval. | ||
* @param {...any} args - The arguments for the throttled function. This can include: | ||
* @param {...any} arguments_ - The arguments for the throttled function. This can include: | ||
* - `fn` (Function): The function to be throttled. | ||
* - `options` (object, optional): Throttling options. | ||
* - `options.interval` (number|string, optional): The time interval in milliseconds between function calls. | ||
|
@@ -24,8 +24,9 @@ module.exports = throttle; | |
* | ||
* Credit: Original inspiration from https://github.com/brianloveswords/throttle-function "Brian J Brennan" <[email protected]> | ||
*/ | ||
function throttle(...args) { | ||
const [thisArg, fn, options = {}] = args.length > 1 ? args : [null, ...args]; | ||
function throttle(...arguments_) { | ||
const [thisArgument, function_, options = {}] = | ||
arguments_.length > 1 ? arguments_ : [null, ...arguments_]; | ||
const msBetweenCalls = getMsBetweenCalls(options); | ||
const queue = []; | ||
let timer; | ||
|
@@ -47,12 +48,12 @@ function throttle(...args) { | |
*/ | ||
function runQueue() { | ||
if (queue.length === 0) clearInterval(timer); | ||
return queue.shift() ? fn.apply(thisArg, queue.shift()) : null; | ||
return queue.shift() ? function_.apply(thisArgument, queue.shift()) : null; | ||
} | ||
|
||
return function (...args) { | ||
queue.push(args); | ||
if (!timer) timer = setInterval(runQueue, msBetweenCalls); | ||
return function (...arguments_) { | ||
queue.push(arguments_); | ||
timer ||= setInterval(runQueue, msBetweenCalls); | ||
|
||
return { | ||
position: queue.length, | ||
|
Oops, something went wrong.