Skip to content

Commit

Permalink
docs: Update 7guis-timer example to support SSR context (#13356)
Browse files Browse the repository at this point in the history
* Update 7guis-timer

* remove window reference on variable init

* better still

* prettier

---------

Co-authored-by: Rich Harris <[email protected]>
Co-authored-by: Rich Harris <[email protected]>
  • Loading branch information
3 people authored Sep 23, 2024
1 parent c5fa0b4 commit c5028a2
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions documentation/examples/20-7guis/04-7guis-timer/App.svelte
Original file line number Diff line number Diff line change
@@ -1,31 +1,30 @@
<!-- https://eugenkiss.github.io/7guis/tasks#timer -->

<script>
import { onDestroy } from 'svelte';
import { onMount } from 'svelte';
let elapsed = 0;
let duration = 5000;
let last_time = window.performance.now();
let frame;
onMount(() => {
let last_time = performance.now();
(function update() {
frame = requestAnimationFrame(update);
let frame = requestAnimationFrame(function update(time) {
frame = requestAnimationFrame(update);
const time = window.performance.now();
elapsed += Math.min(time - last_time, duration - elapsed);
elapsed += Math.min(time - last_time, duration - elapsed);
last_time = time;
});
last_time = time;
})();
onDestroy(() => {
cancelAnimationFrame(frame);
return () => {
cancelAnimationFrame(frame);
};
});
</script>

<label>
elapsed time:
<progress value={elapsed / duration} />
<progress value={elapsed / duration}></progress>
</label>

<div>{(elapsed / 1000).toFixed(1)}s</div>
Expand Down

0 comments on commit c5028a2

Please sign in to comment.