1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00
lens/src/renderer/utils/interval.ts
Panu Horsmalahti dcf253e7d5
Add eslint rule padding-line-between-statements (#1593)
Signed-off-by: Panu Horsmalahti <phorsmalahti@mirantis.com>
2020-12-02 09:55:52 +02:00

36 lines
838 B
TypeScript

// Helper for working with time updates / data-polling callbacks
type IntervalCallback = (count: number) => void;
export function interval(timeSec = 1, callback: IntervalCallback, autoRun = false) {
let count = 0;
let timer = -1;
let isRunning = false;
const intervalManager = {
start (runImmediately = false) {
if (isRunning) return;
const tick = () => callback(++count);
isRunning = true;
timer = window.setInterval(tick, 1000 * timeSec);
if (runImmediately) tick();
},
stop () {
count = 0;
isRunning = false;
clearInterval(timer);
},
restart (runImmediately = false) {
this.stop();
this.start(runImmediately);
},
get isRunning() {
return isRunning;
}
};
if (autoRun) intervalManager.start();
return intervalManager;
}