-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Synchronize timers between multiple users (#6885)
* created a throttle util and using it in timer plugin to throttle refreshing the timer domain object * Simplify timer logic * Clarify code a little * refactor: lint:fix * Fix linting issue --------- Co-authored-by: Jamie V <jamie.j.vigliotta@nasa.gov> Co-authored-by: Jesse Mazzella <jesse.d.mazzella@nasa.gov> Co-authored-by: Jesse Mazzella <ozyx@users.noreply.github.com>
- Loading branch information
1 parent
c630569
commit 676bb81
Showing
2 changed files
with
52 additions
and
40 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** | ||
* Creates a throttled function that only invokes the provided function at most once every | ||
* specified number of milliseconds. Subsequent calls within the waiting period will be ignored. | ||
* @param {Function} func The function to throttle. | ||
* @param {number} wait The number of milliseconds to wait between successive calls to the function. | ||
* @return {Function} Returns the new throttled function. | ||
*/ | ||
export default function throttle(func, wait) { | ||
let timeout; | ||
let result; | ||
let previous = 0; | ||
|
||
return function (...args) { | ||
const now = new Date().getTime(); | ||
const remaining = wait - (now - previous); | ||
|
||
if (remaining <= 0 || remaining > wait) { | ||
if (timeout) { | ||
clearTimeout(timeout); | ||
timeout = null; | ||
} | ||
|
||
previous = now; | ||
result = func(...args); | ||
} else if (!timeout) { | ||
timeout = setTimeout(() => { | ||
previous = new Date().getTime(); | ||
timeout = null; | ||
result = func(...args); | ||
}, remaining); | ||
} | ||
return result; | ||
}; | ||
} |