BlueGit

blue-star danique-bot

Files

danique-bot / node_modules / throttleit / index.js

index.js

Import template
timfa Tim · pushed by timfa · 4 years ago
c788ad20 History
index.js 820 B Download Raw
1module.exports = throttle;
2
3/**
4 * Returns a new function that, when invoked, invokes `func` at most once per `wait` milliseconds.
5 *
6 * @param {Function} func Function to wrap.
7 * @param {Number} wait Number of milliseconds that must elapse between `func` invocations.
8 * @return {Function} A new function that wraps the `func` function passed in.
9 */
10
11function throttle (func, wait) {
12 var ctx, args, rtn, timeoutID; // caching
13 var last = 0;
14
15 return function throttled () {
16 ctx = this;
17 args = arguments;
18 var delta = new Date() - last;
19 if (!timeoutID)
20 if (delta >= wait) call();
21 else timeoutID = setTimeout(call, wait - delta);
22 return rtn;
23 };
24
25 function call () {
26 timeoutID = 0;
27 last = +new Date();
28 rtn = func.apply(ctx, args);
29 ctx = null;
30 args = null;
31 }
32}