0

double-triggering

there is a event "onclick" assigned on the item. How to make so that it was impossible to call this function again until it has finished working? m.onclick=setInterval(run,10); run(){};

2nd Jan 2018, 7:42 AM
Илья Ланин
Илья Ланин - avatar
2 Answers
+ 1
The cheap way out is to use a debouncer that checks how much time has passed since it last ran. function debounce(fn, timeout){ let time = 0; return function(...args){ if(Date.now() > time + timeout){ time = Date.now(); return fn(...args); } }; }; now you can run_debounced = debounce(run, 10); m.onclick = run_debounced; And your run function will run at most once every 10 milliseconds. It's not exactly what you asked for but usually good enough. By the way, .onclick is bad practice. Use `m.addEventListener('click', run_debounced);` instead, that way you can add more than one onclick handler if needed.
2nd Jan 2018, 10:22 AM
Schindlabua
Schindlabua - avatar
0
Thanks for the help. About addEventListener is the question. You are not the first who says that onclick is unable to perform several functions. I just assign a few functions... onclick=setInterval(run1,10)=setInterval(ran2,10)=setInterval(ran3,100);
2nd Jan 2018, 2:14 PM
Илья Ланин
Илья Ланин - avatar