I have a main function with a setTimeout function inside of it that uses a random interval each time the main function is called. Once the timeout triggers it will run the main function again and create a new timeout. However I can manually run the main function multiple times, continuously generating additional randomly timed setTimeouts. I don't want this to happen, I need the timeouts to overwrite each other each time they are made so that only one can exist at a time. I have applied a couple of fixes already which seem to work in theory but in practice fail. Here is my code:
mainFunc {
var timeout;
console.log(timeout)
clearTimeout(timeout);
timeout = setTimeout(mainFunc(), randomTime)
}
As you can see clearing the timeouts and restating the variable both fail to change the behavior of the program. Any ideas are greatly appreciated.