javascript - Onclick reset setInterval -
<!doctype html> <html> <head> <script> function timer(x) { if (x == 1) { //reset timer } var = 0; var timer = setinterval(function() { var x = document.getelementbyid("demo").innerhtml = i; = + 1; }, 500); } </script> </head> <body> <p id="demo">hello</p> <button type="button" onclick="timer(0)">change</button> <button type="button" onclick="timer(1)">reset</button> </body> </html>
i want reset timer onclick . e.g. if setintervaltime set 5 sec , 3 seconds elapsed ,after if on click on reset button should start gain 5 seconds.how this.
- keep return value of
settimeout
somewhere can again (currently storing in local variable, goes away when function ends) - call
cleartimeout(timer);
- call
settimeout
whatever arguments want again.
Comments
Post a Comment