UNPKG

@wix/design-system

Version:

@wix/design-system

41 lines 1.25 kB
var TimeoutState; (function (TimeoutState) { TimeoutState[TimeoutState["PAUSED"] = 0] = "PAUSED"; TimeoutState[TimeoutState["RUNNING"] = 1] = "RUNNING"; TimeoutState[TimeoutState["CLEARED"] = 2] = "CLEARED"; })(TimeoutState || (TimeoutState = {})); export const createTimeout = (callback, delay) => { let timeoutId; let state = TimeoutState.PAUSED; let timeElapsed = 0; let timeStart; const handleTimeout = () => { state = TimeoutState.CLEARED; callback(); }; const pause = () => { if (state !== TimeoutState.RUNNING) { return; } state = TimeoutState.PAUSED; timeElapsed += Date.now() - timeStart; clearTimeout(timeoutId); }; const resume = () => { if (state !== TimeoutState.PAUSED) { return; } state = TimeoutState.RUNNING; timeStart = Date.now(); timeoutId = setTimeout(handleTimeout, delay - timeElapsed); }; const clear = () => { if (state === TimeoutState.CLEARED) { return; } state = TimeoutState.CLEARED; clearTimeout(timeoutId); }; return { pause, resume, clear }; }; //# sourceMappingURL=pauseableTimeout.js.map