jQuery delayed events.

February 1, 2013

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.

tags: , , ,
posted in jQuery, js, Plugins by Ivan Gospodinow

Follow comments via the RSS Feed | Leave a comment | Trackback URL

Leave Your Comment


Warning: Use of undefined constant XML - assumed 'XML' (this will throw an Error in a future version of PHP) in /home/c2kblate/sites/ivangospodinow.com/wp-content/plugins/wp-syntaxhighlighter/wp-syntaxhighlighter.php on line 1048
 
Powered by Wordpress and MySQL. Theme by Shlomi Noach, openark.org