onDelay is simple jquery plugin that trigger events with delay.
/**
* Trigger event delayed.
* @author Ivan Gospodinow
* @site http://www.ivangospodinow.com
* @mail ivangospodinow@gmail.com
* @date 01.02.2013
*/
(function($) {
$.fn.onDelay = function(type, delay, funct) {
delay = undefined === delay ? 500 : parseInt(delay);
var timeOut;
$(this).unbind(type)[type](function(e,x,y,z) {
clearTimeout(timeOut);
var self = this;
timeOut = setTimeout(function(){
funct.call(self,e,x,y,z);
},delay);
});
};
})(jQuery);
How to use it ?
(function($) {
$(window).onDelay('click', 2000, function(e) {
console.log('Sorry , I am late.');
})
})(jQuery);
UPDATED – Now supports $(this) in chield function.
UPDATED 25.11.2018
/**
* Trigger event delayed.
* @author Ivan Gospodinow
* @site http://www.ivangospodinow.com
* @mail ivangospodinow@gmail.com
* @date 25.11.2018
*/
(function($) {
$.fn.onDelay = function(events, selector, funct, delay) {
var timeout;
var callback = function (e) {
clearTimeout(timeout);
timeout = setTimeout(funct.bind(this, e), delay || 500);
}
$(this).off(events, selector, callback)
.on(events, selector, callback);
};
})(jQuery);
Suggestions or problems ?
Write a comment.