Last active
August 1, 2021 11:16
-
-
Save noahub/c530eb73b31af677e3db9a80762acdf1 to your computer and use it in GitHub Desktop.
Add A countdown timer to your landing page
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
<style> | |
.done {color: tomato !important;} | |
</style> |
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
<script> | |
var startTime = 0.25; //set countdown in Minutes | |
var doneClass = "done"; //optional styling when timer is done | |
function startTimer(duration, display) { | |
var timer = duration, minutes, seconds; | |
var intervalLoop = setInterval(function () { | |
minutes = parseInt(timer / 60, 10) | |
seconds = parseInt(timer % 60, 10); | |
minutes = minutes < 10 ? "0" + minutes : minutes; | |
seconds = seconds < 10 ? "0" + seconds : seconds; | |
for(var i=0;i<display.length;i++){ | |
display[i].textContent = minutes + ":" + seconds; | |
} | |
if (--timer < 0) { | |
for(var i=0;i<display.length;i++){ | |
display[i].classList.add(doneClass); | |
display[i].textContent = "DONE"; | |
} | |
clearInterval(intervalLoop); | |
} | |
}, 1000); | |
} | |
window.onload = function () { | |
var setMinutes = 60 * startTime, | |
display = document.querySelectorAll(".timer"); | |
startTimer(setMinutes, display); | |
}; | |
</script> |
flkasper
commented
Mar 20, 2020
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment