mirror of
https://scm.univ-tours.fr/22107988t/rappaurio-sae501_502.git
synced 2025-08-29 17:55:58 +02:00
v1.0 du site web
This commit is contained in:
33
app/node_modules/axios/lib/helpers/throttle.js
generated
vendored
Normal file
33
app/node_modules/axios/lib/helpers/throttle.js
generated
vendored
Normal file
@@ -0,0 +1,33 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* Throttle decorator
|
||||
* @param {Function} fn
|
||||
* @param {Number} freq
|
||||
* @return {Function}
|
||||
*/
|
||||
function throttle(fn, freq) {
|
||||
let timestamp = 0;
|
||||
const threshold = 1000 / freq;
|
||||
let timer = null;
|
||||
return function throttled(force, args) {
|
||||
const now = Date.now();
|
||||
if (force || now - timestamp > threshold) {
|
||||
if (timer) {
|
||||
clearTimeout(timer);
|
||||
timer = null;
|
||||
}
|
||||
timestamp = now;
|
||||
return fn.apply(null, args);
|
||||
}
|
||||
if (!timer) {
|
||||
timer = setTimeout(() => {
|
||||
timer = null;
|
||||
timestamp = Date.now();
|
||||
return fn.apply(null, args);
|
||||
}, threshold - (now - timestamp));
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export default throttle;
|
Reference in New Issue
Block a user