
This is a collection of functions for working with timeouts.
The setTimeout function sets a timeout to run a function after ms milliseconds. When calling setTimeout with only the function and the time, the function will be used as the key.
The function passed to the setInterval function must be constant, otherwise you will not be able to clear the interval.
import { clearTimeout, setTimeout } from '@aracna/core';
function fn() {
console.log('running', Date.now());
}
setTimeout(fn, 1000);
console.log('timeout set', Date.now());
setTimeout(() => {
setTimeout(fn, 1000);
// will not run anymore after 1s
clearTimeout(fn);
}, 2000);The setTimeout function can also be called with a custom key. This is useful when you want to clear a timeout with a function that is not constant.
import { clearTimeout, setTimeout } from '@aracna/core';
const key = 'timeout';
setTimeout(() => console.log('running t1', Date.now()), 1000, key);
console.log('timeout set', Date.now());
setTimeout(() => {
setTimeout(() => console.log('running t2', Date.now()), 1000, key);
// will not run anymore after 1s
clearTimeout(key);
}, 2000);The clearTimeout function clears a timeout.
import { clearTimeout, setTimeout } from '@aracna/core';
function fn() {
console.log('running', Date.now());
}
setTimeout(fn, 1000);
// will not run anymore
clearTimeout(fn);The clearEveryTimeout function clears all timeouts.
import { clearEveryTimeout, setTimeout } from '@aracna/core';
setTimeout(() => console.log('running t1', Date.now()), 1000);
setTimeout(() => console.log('running t2', Date.now()), 1000);
// both timeouts will not run
clearEveryTimeout();The isTimeoutSet function checks if a timeout is set.
import { setTimeout, isTimeoutSet } from '@aracna/core';
function fn() {
console.log('running', Date.now());
}
setTimeout(fn, 1000);
// will log true
console.log(isTimeoutSet(fn));