Last active
November 3, 2019 06:59
-
-
Save luqmansen/05c2943d2583086e91a580d3cce2e435 to your computer and use it in GitHub Desktop.
Simple Js Countdown 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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Countdown Timer</title> | |
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous"> | |
</head> | |
<body> | |
<div class="text-center text-xs-center"> | |
<h2>Countdown Timer</h2> | |
<form id="form"> | |
<input type="text" placeholder="input the time in second" id="timerCount" /> | |
<input type="button" value="submit" onclick="getData()" /> | |
</form> | |
<h2 id="timer">00:00</h2> | |
</div> | |
</body> | |
<script> | |
function timer(duration, show) { | |
var timer = duration, minutes, seconds; | |
var interval = setInterval(function () | |
{ | |
if (--timer < 0) { | |
timer = 0; | |
clearInterval(interval); | |
alert("Times up"); | |
} | |
minutes = parseInt(timer / 60, 10); | |
seconds = parseInt(timer % 60, 10); | |
minutes = minutes < 10 ? "0" + minutes : minutes; | |
seconds = seconds < 10 ? "0" + seconds : seconds; | |
show.textContent = minutes + ":" + seconds; | |
}, 1000); | |
} | |
function getData() { | |
let time = document.getElementById("timerCount").value; | |
time = parseInt(time) | |
time += 1 | |
console.log(time) | |
show = document.querySelector("#timer"); | |
timer(time, show); | |
} | |
</script> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment