Creating a timer in JavaScript is quite straightforward. Here’s a simple example of how you can create a countdown timer:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Countdown Timer</title>
</head>
<body>
<div id="timer">10:00</div>
<script>
// Set the countdown time in seconds
let time = 600; // 10 minutes
// Function to update the timer
function updateTimer() {
let minutes = Math.floor(time / 60);
let seconds = time % 60;
// Add leading zeros if needed
minutes = minutes < 10 ? '0' + minutes : minutes;
seconds = seconds < 10 ? '0' + seconds : seconds;
// Display the timer
document.getElementById('timer').textContent = minutes + ':' + seconds;
// Decrease the time by one second
time--;
// Stop the timer when it reaches zero
if (time < 0) {
clearInterval(timerInterval);
document.getElementById('timer').textContent = "Time's up!";
}
}
// Update the timer every second
let timerInterval = setInterval(updateTimer, 1000);
</script>
</body>
</html>
This code sets up a simple countdown timer that starts from 10 minutes and updates every second. When the timer reaches zero, it stops and displays “Time’s up!”.
Feel free to ask if you need any more details or have other questions!
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Creating a timer in JavaScript is quite straightforward. Here’s a simple example of how you can create a countdown timer:
This code sets up a simple countdown timer that starts from 10 minutes and updates every second. When the timer reaches zero, it stops and displays “Time’s up!”.
Feel free to ask if you need any more details or have other questions!