wow, this is my first post! New to programming and coding, this is something of my first projects I work on. I would like to be able to switch a toggle to run a code or to stop it, using the setInterval() for it. But now I am not able to switch it off when selecting the toggle. Tried multiple thing, like break; but not successful so far. Would be great if any of you could point me in the right direction.
Kind regards, Laurens
Code in HTML
<card>
<p>Theft Script ON/OFF</p>
<label class="switch">
<input id="executeTheftScript" type="checkbox" > Toggle me
</label>
</card>
Code in Javascript
function theftToggle(){
var isChecked = document.getElementById("executeTheftScript").checked;
if (isChecked) {
var i = setInterval(function(){
if (person1.credits === 0) {
clearInterval(i);
updateStats();
console.log("You don't have any credits.");
} else {
theft();
person1.credits--;
updateStats();
};
}, 500);
} else {
console.log("Your script is stopped.");
}
}
document.getElementById("executeTheftScript").addEventListener('click', theftToggle);
Running of the code;