@magnetarjs/utils-firestore
Version:
Magnetar utils firestore
43 lines (42 loc) • 1.4 kB
JavaScript
/**
* A countdown which can be restarted and resolves when the provided milliseconds have passed.
*
* @param {number} ms The amount of milliseconds to count down.
* @returns {{done: Promise<void>, restart: (newDurationMs?: number) => void}} restart will reset the countdown and start counting down again.
* @example
* const countdown = Countdown(1000)
* // set up what to do when it's finished:
* countdown.done.then(() => doSomething())
* // call this every time to restart the countdown:
* countdown.restart()
* @author Adam Dorling
* @contact https://codepen.io/naito
*/
export function Countdown(ms) {
let startTime = Date.now();
let interval = null;
let resolveTrigger = null;
let duration = ms;
const done = new Promise((resolve) => (resolveTrigger = resolve));
function finish() {
clearInterval(interval);
interval = null;
if (resolveTrigger) {
resolveTrigger();
resolveTrigger = null;
}
}
function restart(newDurationMs) {
if (typeof newDurationMs !== 'undefined') {
duration = newDurationMs;
}
startTime = Date.now();
}
interval = setInterval(() => {
const now = Date.now();
const deltaT = now - startTime;
if (deltaT >= duration)
finish();
}, 10);
return { done, restart, forceFinish: finish };
}