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(){};
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.
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);