frontend/classes/Ticker.ts
2025-10-26 18:56:20 +01:00

31 lines
861 B
TypeScript

export function useTicker(interval: string)
{
const seconds = Duration.parse(interval);
const ticker = ref(new Date());
setInterval(() => {
ticker.value = new Date();
}, seconds * 1000);
return ticker;
}
export class Duration
{
static parse(input: string): number {
if (input.length > 1) {
const unit = input.charAt(input.length - 1);
const value = parseInt(input.substring(0, input.length - 1), 10);
switch (unit)
{
case 's':
return value;
case 'm':
return value * 60;
case 'h':
return value * 60 * 60;
default:
throw new Error(`Invalid unit ${unit}`);
}
}
throw new Error("Invalid input");
}
}