File size: 906 Bytes
a417977
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// export function throttle(callback: () => void, wait: number): () => void {
//     if (typeof function_ !== 'function') {
//         throw new TypeError(`Expected the first argument to be a \`function\`, got \`${typeof function_}\`.`);
//     }
//
//     let timeoutId;
//     let lastCallTime = 0;
//
//     return function throttled(...arguments_) {
//         clearTimeout(timeoutId);
//
//         const now = Date.now();
//         const timeSinceLastCall = now - lastCallTime;
//         const delayForNextCall = wait - timeSinceLastCall;
//
//         if (delayForNextCall <= 0) {
//             lastCallTime = now;
//             function_.apply(this, arguments_);
//         } else {
//             timeoutId = setTimeout(() => {
//                 lastCallTime = Date.now();
//                 function_.apply(this, arguments_);
//             }, delayForNextCall);
//         }
//     };
// }