ravi
answered Apr 27 '22 00:00
Example of clearInterval() method :
keep changing the HTML body background color until click at anywhere in the page by clearInterval() and setInterval() method
const ChangeBodyColorintervalid = setInterval(ChangeBodyColor, 500);
function ChangeBodyColor() {
document.body.style.backgroundColor = ( document.body.style.backgroundColor == "red" ? "black" : "red");
}
document.body.addEventListener("click", function () {
clearInterval(ChangeBodyColorintervalid);
}, false);