0

why is setInterval doing a loop?

This is an html page with some simple javascript. The alert pops up again and again after clicking the ok button. I don't understand. I can see setInterval runs the function after 3 seconds pass. The alert pops up I close the alert so that should be the end. I clicked on the "Try it" button only once. If there is a loop I don't see it. // This is the code with the loop in question. <button onclick="myFunction()">Try it</button> <script> var myVar; function myFunction() { myVar = setInterval(alertFunc, 3000); } function alertFunc() { alert("Hello!"); } </script>

31st Aug 2019, 10:45 PM
Elvis Delahoz
Elvis Delahoz - avatar
2 ответов
+ 2
setTimeout(function, milliseconds) Executes a function, after waiting a specified number of milliseconds. setInterval(function, milliseconds) Same as setTimeout(), but repeats the execution of the function continuously.
31st Aug 2019, 10:58 PM
rodwynnejones
rodwynnejones - avatar
0
https://www.w3schools.com/jsref/met_win_setinterval.asp setInterval is the loop. setInterval repeats the the function every x milliseconds until clearInterval is called. You want setTimeout
31st Aug 2019, 10:54 PM
jay
jay - avatar