UNPKG

svelte-settings

Version:

> [!WARNING] > This project is a work in progress. Do not use it in any of your projects yet.

39 lines (38 loc) 1.14 kB
export function debounce(callback, wait, immediate = false) { let timeout = null; const debounced = (...args) => { const shouldCallNow = immediate && !timeout; if (timeout) { clearTimeout(timeout); } timeout = setTimeout(() => { timeout = null; if (!shouldCallNow) { callback(...args); } }, wait); if (shouldCallNow) { callback(...args); } }; return debounced; } export function throttle(callback, wait) { let timeout; let lastTime = 0; const throttled = (...args) => { clearTimeout(timeout); const delay = Math.max(wait - (Date.now() - lastTime), 0); timeout = setTimeout(() => { if (Date.now() - lastTime >= wait) { callback(...args); lastTime = Date.now(); } }, delay); }; return throttled; } // NOTE: crypto.randomUUID is not available over http export function createUUID() { return crypto?.randomUUID ? crypto.randomUUID() : Math.random().toString(36).slice(2) + Date.now(); }