1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00
lens/dashboard/client/utils/interval.ts
Jari Kolehmainen d057cb17c6 Lens app source code (#119)
Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>
Signed-off-by: Cody Belcher <cody.t.belcher@gmail.com>
2020-04-16 11:23:35 -05:00

33 lines
864 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: function (runImmediately = false) {
if (isRunning) return;
const tick = () => callback(++count);
isRunning = true;
timer = window.setInterval(tick, 1000 * timeSec);
if (runImmediately) tick();
},
stop: function () {
count = 0;
isRunning = false;
clearInterval(timer);
},
restart: function (runImmediately = false) {
this.stop();
this.start(runImmediately);
},
get isRunning() {
return isRunning;
}
}
if (autoRun) intervalManager.start();
return intervalManager;
}