generate functionMon, 20 Mar 2023

Create a stopper in Javascript! Create a start and stop button in HTML(use createElement). Click the start button to start the counter and click the stop button to stop it. The start button uses setInterval, the stop bomb uses clearInterval.

const startBtn = document.createElement('button'); startBtn.textContent = 'start'; document.body.appendChild(startBtn); const stopBtn = document.createElement('button'); stopBtn.textContent = 'stop'; document.body.appendChild(stopBtn); let i = 0; const interval = setInterval(function() { console.log('hello', i); i++; }, 1000); startBtn.addEventListener('click', function() { interval; }); stopBtn.addEventListener('click', function() { clearInterval(interval); });

Generate More

Questions about programming?Chat with your personal AI assistant