<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>setInterval counter</title> </head> <body> <h1 id="timer"></h1> <button type="button" onclick="startInterval();">Start interval</button> <script type="text/javascript"> "use strict" let n=60; // in seconds let i; function startInterval(){ i=setInterval(()=>{ document.getElementById("timer").innerHTML=n; if(n==0){ clearInterval(i); // switch state for states } n=n-1; },100); } </script> </body> </html>