-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththrottle.js
51 lines (48 loc) · 1.61 KB
/
throttle.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/**
* Returns a function that, as long as it is called,
* it run only once every N milliseconds.
*
* @param {Function} func The function to control
* @param {Number} wait The number of milliseconds to wait before to run the func function.
* @param {}
* @param {*?} context The context in which to run func() (default value is `this`)
*
* - leading (optionnel) : Appeler également func() à la première
* invocation (Faux par défaut)
* - trailing (optionnel) : Appeler également func() à la dernière
* invocation (Faux par défaut)
* - context (optionnel) : le contexte dans lequel appeler func()
* (this par défaut)
*/
function throttle(func, wait, leading, trailing, context) {
var ctx, args, result;
var timeout = null;
var previous = 0;
var later = function() {
previous = new Date;
timeout = null;
result = func.apply(ctx, args);
};
return function() {
var now = new Date;
if (!previous && !leading) previous = now;
var remaining = wait - (now - previous);
ctx = context || this;
args = arguments;
// Si la période d'attente est écoulée
if (remaining <= 0) {
// Réinitialiser les compteurs
clearTimeout(timeout);
timeout = null;
// Enregistrer le moment du dernier appel
previous = now;
// Appeler la fonction
result = func.apply(ctx, args);
} else if (!timeout && trailing) {
// Sinon on s’endort pendant le temps restant
timeout = setTimeout(later, remaining);
}
return result;
};
};
export default throttle;