I am developing a custom scrollbar, and for performance reasons I want to add debounce
to a mousemove
event. The problem is the event object doesn't get passed to the handler and I get undefined
.
here is what i wanna do:
function myFunc (a, b, event) {some code}
element.on('mousemove' , debounce(myfunc));
doesn't work! I also tried:
let myDBFunc = debounce(myFunc, 200)
element.on('mousemove', function(event){
myDBFunc(a, b, event);
});
nothing! any ideas?
UPDATE: the debounce function:
function debounce(func, wait = 20,immediate = true){
let timeout;
return function() {
let context = this, args = arguments;
let later = function () {
timeout = null;
if(!immediate) func.apply(context, args);
};
let callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) func.apply(context.args) ; //edited to func.apply(context, args);
};
}