Skip to content

Instantly share code, notes, and snippets.

@graphicbeacon
Created July 6, 2018 23:11
Show Gist options
  • Save graphicbeacon/85c016bc27c10158ffca95b3ff09b0fd to your computer and use it in GitHub Desktop.
Save graphicbeacon/85c016bc27c10158ffca95b3ff09b0fd to your computer and use it in GitHub Desktop.
Code for a timer
<span class="clock"></span>
import 'dart:async';
import 'dart:html';
void main() {
var hours = 0, minutes = 0, seconds = 0;
updateClock(hours, minutes, seconds);
Timer.periodic(Duration(seconds: 1), (_) {
seconds++;
if (seconds >= 60) {
seconds = 0;
minutes++;
if (minutes >= 60) {
minutes = 0;
hours++;
}
}
updateClock(hours, minutes, seconds);
});
}
updateClock(hours, minutes, seconds) {
var h = hours, m = minutes, s = seconds;
if (hours < 10) h = '0$hours';
if (minutes < 10) m = '0$minutes';
if (seconds < 10) s = '0$seconds';
querySelector('.clock')
..innerHtml = '$h:$m:$s';
}
.clock {
color: white;
margin-bottom: 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment