Created
October 14, 2013 08:10
-
-
Save NOEinteractive/6972481 to your computer and use it in GitHub Desktop.
Debounce & End event
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;(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)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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 | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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