There is an element (button) that is clicked many times, but the function must be called once. Tried through setTimeout, did not work (Probably bad tried).
1 answer
I think you meant something like this:
var timeout = 3000; var lastCall = null; function action() { if((Date.now() - lastCall) >= timeout) { alert('Action!'); } } function actionWait() { lastCall = new Date(); setTimeout(action, timeout) } <button onclick="actionWait()">button</button> Timeout is set by timeout variable
|
debounce. You can look at the implementation in the underscope or lodash. - Arnial