mirror of
https://github.com/finos/SymphonyElectron.git
synced 2024-12-29 02:11:28 -06:00
439f283916
* saved layout * fix fieldName check
39 lines
929 B
JavaScript
39 lines
929 B
JavaScript
'use strict';
|
|
|
|
/**
|
|
* throttles calls to given function at most once a second.
|
|
* @param {number} throttleTime minimum time between calls
|
|
* @param {function} func function to invoke
|
|
*/
|
|
function throttle(throttleTime, func) {
|
|
let timer, lastInvoke = 0;
|
|
return function() {
|
|
let args = arguments;
|
|
|
|
function invoke(argsToInvoke) {
|
|
timer = null;
|
|
lastInvoke = Date.now();
|
|
func.apply(null, argsToInvoke);
|
|
}
|
|
|
|
function cancel() {
|
|
if (timer) {
|
|
clearTimeout(timer);
|
|
}
|
|
}
|
|
|
|
let now = Date.now();
|
|
if (now - lastInvoke < throttleTime) {
|
|
cancel();
|
|
timer = setTimeout(function() {
|
|
invoke(args);
|
|
}, lastInvoke + throttleTime - now);
|
|
} else {
|
|
cancel();
|
|
invoke(args);
|
|
}
|
|
}
|
|
}
|
|
|
|
module.exports = throttle;
|