source: node_modules/es-toolkit/dist/function/throttle.js

Last change on this file was a762898, checked in by istevanoska <ilinastevanoska@…>, 5 months ago

Added visualizations

  • Property mode set to 100644
File size: 931 bytes
Line 
1'use strict';
2
3Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
5const debounce = require('./debounce.js');
6
7function throttle(func, throttleMs, { signal, edges = ['leading', 'trailing'] } = {}) {
8 let pendingAt = null;
9 const debounced = debounce.debounce(function (...args) {
10 pendingAt = Date.now();
11 func.apply(this, args);
12 }, throttleMs, { signal, edges });
13 const throttled = function (...args) {
14 if (pendingAt == null) {
15 pendingAt = Date.now();
16 }
17 if (Date.now() - pendingAt >= throttleMs) {
18 pendingAt = Date.now();
19 func.apply(this, args);
20 debounced.cancel();
21 debounced.schedule();
22 return;
23 }
24 debounced.apply(this, args);
25 };
26 throttled.cancel = debounced.cancel;
27 throttled.flush = debounced.flush;
28 return throttled;
29}
30
31exports.throttle = throttle;
Note: See TracBrowser for help on using the repository browser.