Skip to content

Instantly share code, notes, and snippets.

@NOEinteractive
Created October 14, 2013 08:10
Show Gist options
  • Save NOEinteractive/6972481 to your computer and use it in GitHub Desktop.
Save NOEinteractive/6972481 to your computer and use it in GitHub Desktop.
Debounce & End event
;(function($, $w) {
var isScrolling = false,
timerScroll = null;
function _sc() { //ici, event scroll, on ne fait aucun gros traitement, pas d'accès au DOM rien..
if(!isScrolling) {
isScrolling = true;
scrollRAF();
}
if(timerScroll) {
clearTimeout(timerScroll);
timerScroll = null;
}
timerScroll = setTimeout(function() {
isScrolling = false;
}, 400);
};
function scrollRAF() {
//ici faire tous les traitements qu'on veut au scroll, l'event se déclenchera que 5 fois par secondes
isScrolling && setTimeout(scrollRAF, 1000 / 5);
};
$w.on('scroll', _sc);
})(jQuery, jQuery(window));
(function($, $w){
var timeout;
$w.on('resize', function() {
if(timeout) { clearTimeout(timeout); timeout = null; }
timeout = setTimeout(function() { $w.trigger('endresize');}, 200);
});
})(jQuery, jQuery(window));
//dispatch un event endresize à la fin du resize, plutot que une centaine d'event pendant que l'user resize :
$(window).on('endresize', function() {
//faire ses trucs
});
(function($, $w){
var timeout;
$w.on('scroll', function() {
if(timeout) { clearTimeout(timeout); timeout = null; }
timeout = setTimeout(function() { $w.trigger('endscroll');}, 200);
});
})(jQuery, jQuery(window));
//dispatch un event endscroll à la fin du scroll, plutot que une centaine d'event pendant que l'user scroll :
$(window).on('endscroll', function() {
//faire ses trucs
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment