Duration
Durations let you work with time in your app. Think of them as a simple way to say “wait 5 seconds” or “show this for 2 minutes.”
How Durations Work
In Rhyme, you write durations as a number followed by a time unit:
// Basic duration examples
$quick-pause = 2s // 2 seconds
$loading-time = 500ms // 500 milliseconds (half a second)
$lunch-break = 1h // 1 hour
Time Units You Can Use
Here are all the time units available:
ms
- milliseconds (1/1000 of a second)s
- secondsm
- minutesh
- hoursd
- daysw
- weeksy
- years (365 days)
// Different ways to express time
$blink = 100ms // Quick blink
$countdown = 10s // 10 second countdown
$meeting = 30m // Half hour meeting
$workday = 8h // 8 hour workday
$weekend = 2d // 2 day weekend
$vacation = 2w // 2 week vacation
Working with Durations
You can compare durations in your app:
// Comparing durations
if $user-time < 5s
.text "That was fast!"
endif
// Check if time is up
if $elapsed >= 30s
.text "Time's up!"
endif
Real-World Examples
Here are some common ways to use durations:
// Show a welcome message briefly
.text "Welcome!"
wait 3s
.clear
// Create a simple timer
$timer = 1m
.text "You have $timer to complete the task"
// Control animations
.animate fade-in 500ms
// Set timeouts
timeout 30s
.text "Time's up!"
end
Tips for Beginners
- Start simple: Use seconds (
s
) for most things when you’re learning - Be precise: Use milliseconds (
ms
) for quick animations - Think naturally: Use the unit that makes sense -
2h
is clearer than7200s
Remember: durations make your app feel more dynamic by controlling when things happen!