A simple timer to measuring time interval implemented swith JavaScript
If you want to contact, mail me or send a message on Twitter
📧 igornoiasilva@gmail.com / ☕ @IgorNoiaSilva
- 💡 Features
- 🎯 Purpose
- 🛠 Installation
- 📝 Utilization
- 🤝 Contributing
- 🧾 License
- 🕒 Measure your time.
- 🛑 You can stop the timer.
- 🔁 You can reset the timer.
My purpose with this project is to
- learn how to use
setInterval
andclearInterval
- learn how to work with dates
- how to use
addEventListener
in a better way - learn more about Javascript.
Run this command to clone the repository:
git clone https://github.com/AleNoia/timer.git
You can start the timer by clicking the Start button.
let seconds = 0 // variable of the time to manipulate
let timer; // variable of the setInterval to use clearInterval
// Return a string formatted
function getTime(sec) {
const date = new Date(sec * 1000);
return date.toLocaleTimeString('pt-BR', {
hour12: false,
timeZone: 'GMT'
});
}
// Insert a string on the HTML and start the timer
function startTimer() {
clearInterval(timer)
timer = setInterval(function () {
seconds++
clock.innerHTML = getTime(seconds);
}, 1000)
}
// Event to start the timer
if (el.classList.contains('start')) {
startTimer()
}
To stop the timer, click on the button Stop
// Event to stop the timer
if (el.classList.contains('stop')) {
clearInterval(timer)
}
To reset the timer, click on the button Reset
// Reset the timer to 00:00:00
function resetTime() {
seconds = 0;
clock.innerHTML = getTime(seconds);
clearInterval(timer)
}
// Event to reset the timer
if (el.classList.contains('reset')) {
resetTime()
}
Feel free to contribute 🙂
Released in 2021. This project is under the MIT license.
Made by Igor Noia 👋