javascript - how to set timeout each time a function is called -
i'm trying create if statement sets timeout when function called:
var timeoutid; if (//function1 called) { timeoutid = settimeout(function1, 30000); } the idea function gets repeatedly called after 30 seconds, timeout can reset @ point if function called (e.g. button clicked). how can accomplished? many thanks.
instead of settimeout use setinterval. call callback function repeatedly every given time , can clear via clearinterval function.
see example
const start = document.getelementbyid('start'); const clear = document.getelementbyid('clear'); let intervalid = ''; start.addeventlistener('click', () => intervalid = setinterval(() => console.log('tick'), 1000)); clear.addeventlistener('click', () => clearinterval(intervalid)); <button id="start">start</button> <button id="clear">clear</button>
Comments
Post a Comment