File size: 747 Bytes
b2ecf7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<script lang="ts">
	import { onDestroy } from "svelte";

	export let isDisabled = false;

	let counterSeconds = 0.0;
	let interval: ReturnType<typeof setInterval>;
	let shouldDisplay = false;

	// timer show in seconds
	$: counterHuman = counterSeconds.toLocaleString(undefined, {
		minimumFractionDigits: 1,
	});

	export function start(): void {
		// reset timer for new run
		stop();
		counterSeconds = 0.0;
		shouldDisplay = true;
		// new run
		interval = setInterval(() => (counterSeconds += 0.1), 100);
	}

	export function stop(): void {
		if (interval) {
			clearInterval(interval);
		}
	}

	onDestroy(() => stop());
</script>

{#if shouldDisplay && !isDisabled}
	<span class="font-mono text-xs text-gray-500">{counterHuman}</span>
{/if}