Created
July 6, 2018 23:11
-
-
Save graphicbeacon/85c016bc27c10158ffca95b3ff09b0fd to your computer and use it in GitHub Desktop.
Code for a timer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<span class="clock"></span> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.clock { | |
color: white; | |
margin-bottom: 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment