I am having trouble looping between 2 colours of the body forever. It only loops once.
I am using the 'enter' key to trigger the loop and the 'space' key to stop the loop.
const red= () => {
document.body.style.backgroundColor = "red";
}
const blue= () => {
document.body.style.backgroundColor = "blue";
}
const both = () => {
setTimeout(() => red(), 1000);
setTimeout(() => blue(), 2000);
}
document.body.addEventListener("keydown", function() {
if (event.keyCode == 13) {
setInterval(both(), 3000);
}
if (event.keyCode == 32) {
clearInterval(both());
}
});
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Looping Colors</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<script src="script.js"></script>
</body>
</html>