setTimeout()및 다음 clearTimeout()과 함께 사용할 수 있습니다 jQuery.data.
$(window).resize(function() {
clearTimeout($.data(this, 'resizeTimer'));
$.data(this, 'resizeTimer', setTimeout(function() {
//do something
alert("Haven't resized in 200ms!");
}, 200));
});
최신 정보
jQuery의 기본 (& ) 이벤트 처리기 를 향상시키기 위해 확장 을 작성했습니다 . 이벤트가 지정된 간격 동안 트리거되지 않은 경우 하나 이상의 이벤트에 대한 이벤트 처리기 함수를 선택한 요소에 연결합니다. 이는 resize 이벤트 등 지연 후에 만 콜백을 실행하려는 경우에 유용합니다.
https://github.com/yckart/jquery.unevent.jsonbind
;(function ($) {
var methods = { on: $.fn.on, bind: $.fn.bind };
$.each(methods, function(k){
$.fn[k] = function () {
var args = [].slice.call(arguments),
delay = args.pop(),
fn = args.pop(),
timer;
args.push(function () {
var self = this,
arg = arguments;
clearTimeout(timer);
timer = setTimeout(function(){
fn.apply(self, [].slice.call(arg));
}, delay);
});
return methods[k].apply(this, isNaN(delay) ? arguments : args);
};
});
}(jQuery));
추가 매개 변수를 마지막으로 전달할 수 있다는 점을 제외하고는 다른 on또는 bind-event 핸들러 와 같이 사용하십시오 .
$(window).on('resize', function(e) {
console.log(e.type + '-event was 200ms not triggered');
}, 200);
http://jsfiddle.net/ARTsinn/EqqHx/