I'm currently working with a function that looks like this:
function blabla(){
...
setTimeout(() => {
//do some stuff
}, 10000)
}
My question is, how can I reset the time of the timeout (10000
) if the function was called and the timeout was not finished yet?
I attempted to cancel the timeout if it exists, like so:
function blabla(){
...
if(to){
clearTimeout(to)
}
let to = setTimeout(() => {
//do some stuff
}, 10000)
}
However, I encountered an error indicating that to
is undefined. What is the correct way to determine if a timeout exists or not? Is there a more efficient approach to achieve this task?