Skip to content

Commit 3bfe1e4

Browse files
committed
Add counter example
1 parent 90e1489 commit 3bfe1e4

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

src/App.svelte

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
<script>
2+
import { onMount } from 'svelte'
23
import { v4 as uuid } from 'uuid'
34
import AddTodoItem from './AddTodoItem.svelte'
45
import TodoItem from './TodoItem.svelte'
6+
import getTodos from './getTodos'
7+
import { onInterval } from './interval'
58
6-
let items = [{
7-
id: uuid(),
8-
text: 'First Item',
9-
done: false,
10-
}, {
11-
id: uuid(),
12-
text: 'Second Item',
13-
checked: true,
14-
}]
9+
let items = []
10+
let counter = 0
1511
$: count = items.length;
1612
$: checkedCount = items.filter(({ checked }) => checked).length
13+
14+
onInterval(() => {
15+
counter++
16+
}, 1000)
17+
18+
onMount(async () => {
19+
items = await getTodos()
20+
})
1721
1822
function handleAddTodoItem(event) {
1923
items = [...items, {
@@ -34,6 +38,7 @@
3438
}
3539
</script>
3640

41+
<p>{counter}</p>
3742
<AddTodoItem
3843
title='Please type todo here:'
3944
buttonTitle={`Add (${checkedCount}/${count})`}

src/interval.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { onDestroy } from 'svelte'
2+
3+
export function onInterval(cb, ms) {
4+
const interval = setInterval(cb, ms)
5+
onDestroy(() => {
6+
clearInterval(interval)
7+
})
8+
}

0 commit comments

Comments
 (0)