$(window).scroll(function() { clearTimeout($.data(this, 'scrollTimer')); $.data(this, 'scrollTimer', setTimeout(function() { // do something console.log("Haven't scrolled in 250ms!"); }, 250)); });更新资料
我写了一个扩展来增强jQuery的默认
on-event-handler。它将一个或多个事件的事件处理函数附加到选定的元素,如果在给定间隔内未触发事件,则调用处理函数。如果您只想在延迟(例如resize事件等)之后才触发回调,则此功能很有用。
检查github-repo是否有更新很重要!
https://github.com/yckart/jquery.unevent.js
;(function ($) { var on = $.fn.on, timer; $.fn.on = function () { var args = Array.apply(null, arguments); var last = args[args.length - 1]; if (isNaN(last) || (last === 1 && args.pop())) return on.apply(this, args); var delay = args.pop(); var fn = args.pop(); args.push(function () { var self = this, params = arguments; clearTimeout(timer); timer = setTimeout(function () { fn.apply(self, params); }, delay); }); return on.apply(this, args); };}(this.jQuery || this.Zepto));像其他任何处理程序
on或
bind-event处理程序一样使用它,除了可以在最后传递一个额外的参数外:
$(window).on('scroll', function(e) { console.log(e.type + '-event was 250ms not triggered');}, 250);(此演示使用
resize代替
scroll)



