Best Practice zur Reinitialisierung von Variablen
bearbeitet von Rolf BHallo dedlfix,
> Die Implementation gefällt mir nicht richtig.
Da bin ich ganz bei Dir, aber was ist die Alternative? Zwei verschiedene Debouncer? Eine Weiche, die je nach Wert von immediate unterschiedliche Funktionen bindet?
~~~js
function debounce(func, wait, immediate) {
var timeout, context;
if (immediate) {
return function(...args) {
context = this;
if (!timeout)
func.apply(context, args);
clearTimeout(timeout);
timeout = setTimeout(() => timeout = null, wait);
}
} else {
return function(...args) {
context = this;
clearTimeout(timeout);
timeout = setTimeout(() => {
timeout = null;
func.apply(context, args);
}, wait);
}
}
};
~~~
Das ist jetzt noch ein bisschen modernisiert, mit Lambdas und Rest-Parameter, aber **gefallen** tut mir das auch nicht.
_Rolf_
--
sumpsi - posui - obstruxi