31 lines
861 B
TypeScript
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");
|
|
}
|
|
} |