Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function stopwatch() {
- const currentTime = document.querySelector('#time');
- const startButton = document.querySelector('#startBtn');
- const stopButton = document.querySelector('#stopBtn');
- let time = 0;
- let intervalId;
- const formatTime = (timeValue) => {
- const min = Math.floor(timeValue / 60);
- const sec = timeValue % 60;
- return `${(`0${min}`).slice(-2)}:${(`0${sec}`).slice(-2)}`;
- };
- const startButtonHandler = () => {
- time = 0;
- currentTime.innerText = formatTime(0);
- stopButton.removeAttribute('disabled');
- startButton.setAttribute('disabled', true);
- intervalId = setInterval(() => {
- time++;
- currentTime.innerText = formatTime(time);
- }, 1000);
- };
- const stopButtonHandler = () => {
- stopButton.setAttribute('disabled', true);
- startButton.removeAttribute('disabled');
- clearInterval(intervalId);
- };
- startButton.addEventListener('click', startButtonHandler);
- stopButton.addEventListener('click', stopButtonHandler);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement