[Fixed]-How to stop the accumulation while using setTimeout()?

1👍

Two options:

Option 1:
Set a global value in the beginning of your function. For example window.counterStarted.

But even before you set that value, check if that global value is already set. If so, then don’t start it again.

if(typeof window.counterStarted !== undefined){
    return false;
}
window.counterStarted = true;

Option 2:
Define setTimeout to a global value like window.myTimeout instead of the local variable t within the function. Then to clear the previous timeout before the new one starts, use the following in the beginning of your function:

if(typeof window.myTimeout !== undefined){
    clearTimeout(window.myTimeout);
}

Leave a comment