Created
April 23, 2018 00:18
-
-
Save sergsoares/5d949b1cd0c890e29047bdd121459257 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <script> | |
| function countdown(time) { | |
| setTimeout( ()=> { | |
| if(!time && time == '00:00') { | |
| return | |
| } | |
| let timeInSeconds = convertStringInTime(time) | |
| timeInSeconds-- | |
| let timeAsString = convertTimeInString(timeInSeconds) | |
| showtime(timeAsString) | |
| console.log(timeAsString) | |
| countdown(timeAsString) | |
| }, 1000) | |
| } | |
| function convertStringInTime(time) { | |
| return (parseInt(time.split(':')[0]) * 60) + parseInt(time.split(':')[1]) | |
| } | |
| function convertTimeInString(time) { | |
| let minutes = parseInt(time / 60) | |
| let seconds = time % 60 | |
| if(minutes < 10) { minutes = '0' + minutes } | |
| if(seconds < 10) { seconds = '0' + seconds } | |
| return minutes + ':' + seconds | |
| } | |
| function showtime(time) { | |
| document.getElementById('timer').innerHTML = time | |
| } | |
| function startTime() { | |
| let inputTime = document.querySelector('#input').value | |
| countdown(inputTime) | |
| } | |
| </script> | |
| </head> | |
| <body> | |
| <button onclick="startTime()">Start Timer</button> | |
| <input id="input"/> | |
| <div id="timer"></div> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment