How-to
Website
Web app
Count down
A countdown is a number that drops once a second until it reaches zero. To build one, change a state in an every timer:
Needs: nothing besides the page. It claims /: paste it into a new project, or replace the starter's src/pages/Home.wf with it.
page Countdown(path: "/", title: "Countdown", description: "Ten seconds, counted down.") {
state left = 10
every(1000) { if left > 0 { left = left - 1 } }
Heading("Countdown").h1
Progress(value: 10 - left, max: 10)
Text(if left == 0 { "Done" } else { "{left}s" })
}
How it works
leftis astateholding the seconds that remain.every(1000)runs its block every 1000 milliseconds. Theifstops the count at zero.- The
Progressbar and theTextboth readleft, so they repaint on every tick. The text says Done at zero.
The timer stops when the page leaves, so a reader who navigates away leaves no interval running.