L'anti-tremblement et la limitation sont des méthodes courantes utilisées pour l'optimisation des performances lors du développement frontal:
Fonction anti-tremblement (anti-rebond):
fonction _debounce (fn, delay) {
var delay = retard || 200;
était des heures;
fonction de retour () {
var th = this;
var args = arguments;
if (timer) {
clearTimeout (timer);
}
timer = setTimeout (function () {
timer = null;
fn.apply (th, args);
}, delay);
};
}
Fonction accélérateur (accélérateur):
fonction _throttle (fn, intervalle) {
var last;
var timer;
var interval = interval || 200;
fonction de retour () {
var th = this;
var args = arguments;
var now = + new Date ();
if (last && now - last <interval) {
clearTimeout (timer);
timer = setTimeout (function () {
last = now;
fn.apply (th, args);
}, interval);
} else {
last = now;
fn.apply (th, args);
}
}
}