I'm building a website that allows users to give input, and I want to prevent a function from running before certain time. In this case, I want that time to be 2 seconds. I've been trying to use a setTimeout function to control the delay, and I've been having issues. Through the use of 'console.log()', I've been able to figure out that the issue is that toggleCanRun() is never called. Can anyone explain why? Thank you so much. Here's the code I've got so far
function toggleCanRun () {
canRun = true;
clearTimeout(timer);
};
let timer;
let canRun = true;
function playSound () {
if (canRun === true) {
displayInfo();
canRun = false;
timer = setTimeout(toggleCanRun, 2000);
} else {
clearTimeout(timer);
};
};