JavaScript: setTimeout does NOT require a clearTimeout unless you want to cancel it before it fires (once)
setTimeout does NOT require a clearTimeout unless you want to cancel it before it fires (the first and only time). I checked that out with a small variation of an example given in MDN. My variation is: set-clearTimeout.html (put it up as a Gist). In my variation, console log (as well as page content) can be checked to confirm that setTimeout fires only once and does not fire indefinitely.
The comments in this article seem to be correct (you don't need a clearTimeout unless you want to cancel it): When using setTimeout do you have to clearTimeout?.
In the article: JavaScript clearTimeout() & clearInterval() Method, https://www.geeksforgeeks.org/javascript-cleartimeout-clearinterval-method/ , the statement, "when we use setTimeout() and setInterval() in any JavaScript program then it must clear the time used in those functions so we use the clearTimeout() and clearInterval() methods." seems to be incorrect in the context of setTimeout. It may be correct in the context of setInterval (and clearInterval).
Comments
Post a Comment