i have this simple page:
<!DOCTYPE html> <html> <body> <p>Click the second button to skip the timeout!</p> <button onclick="myStopFunction()">Stop the alert</button> <script> var myVar; spamHi(); function spamHi() { console.log("Hi!"); setTimeout(spamHi,3000); } function myStopFunction() { clearTimeout(spamHi); } </script> </body> </html> The function spamHi() prints "Hi!" in the console every 3 seconds. What the function myStopFunction() is supposed to do is to clear the delay to enable the console to print "Hi!" immediately without waiting the 3 seconds.
How can i fix this?
clearTimeoutorclearIntervalto stop it.