Try this:
function throttle( fn, timeout ) {
var tid = 0;
return function() {
clearTimeout( tid );
var args = [].slice.call( arguments ),
ctx = this;
tid = setTimeout( function() {
fn.apply( ctx, args );
}, timeout );
};
}
$(window).on("scroll", throttle( function() {
$('div').eq(0).append('scroll happened');
}, 100));
It will only fire the scroll once no scroll has happened in 100 milliseconds.
http://jsfiddle.net/NLGHS/1/
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…