UNPKG

debounce-throttling

Version:

debounce-throttle is a lightweight npm package designed to simplify and enhance event handling in JavaScript applications. With this package, developers can effortlessly implement debouncing and throttling functionalities to optimize performance and impro

66 lines (63 loc) 1.92 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var src_exports = {}; __export(src_exports, { debounce: () => debounce, throttle: () => throttle }); module.exports = __toCommonJS(src_exports); // src/debounce-throttle.ts var throttle = (cb, time) => { if (time === void 0) throw new TypeError("time is undefined"); if (time < 0) throw new RangeError("time cannot be negative"); if (typeof cb !== "function") throw new TypeError("callBack is not a function"); let last = 0; return (...args) => { let now = Date.now(); if (now - last >= time) { cb(...args); last = now; } }; }; var debounce = (cb, time) => { if (time === void 0) throw new TypeError("time is undefined"); if (time < 0) throw new RangeError("time cannot be negative"); if (typeof cb !== "function") throw new TypeError("callBack is not a function"); let timer; return (...args) => { if (timer) clearTimeout(timer); timer = setTimeout(() => { cb(...args); }, time); }; }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { debounce, throttle });