Show Output
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>JavaScript Cancel a Timer with clearInterval() Method</title> <style> #clock{ background: #000; padding: 20px; margin: 20px 0; color: lime; display: block; font: 48px monospace; } </style> </head> <body> <script> var intervalID; function showTime() { var d = new Date(); document.getElementById("clock").innerHTML = d.toLocaleTimeString(); } function stopClock() { clearInterval(intervalID); } var intervalID = setInterval(showTime, 1000); </script> <p>The current time on your computer is: <span id="clock"></span></p> <button onclick="stopClock();">Stop Clock</button> </body> </html>