Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using timeouts inside a jQuery plugin while still maintaining chainability

I am trying to create a jQuery plugin that has a timeout function inside of it. This is the basic idea of what I have now. It works fine, but it doesn't maintain chainability.

;(function( $ ) {
    $.fn.myPlugin = function(length) {
        th = $(this);
        th.css('animation', 'none');
        setTimeout((function(th) {
            return function() { 
                th.css('display', 'block');
            };
        })(this), length);
    };
})( jQuery );

To try and make it chainable I created this, but it doesn't run the code in the TimeOut function.

;(function( $ ) {
    $.fn.myPlugin = function(length) {
        return this.each(function() {
            th = $(this);
            th.css('animation', 'none');
            setTimeout((function(th) {
                return function() { 
                    th.css('display', 'block');
                };
            }),(this), length);
        });
    };
})( jQuery );

Here is the plugin working without chaining: http://jsfiddle.net/FhARs/1/

like image 860
Mike Avatar asked Jun 10 '26 04:06

Mike


1 Answers

This one is working, is this what you want ?

;(function( $ ) {
    $.fn.myPlugin = function(length) {
        return this.each(function() {
            th = $(this);
            th.css('display', 'none');
            setTimeout(function() {
                th.css('display', 'block');
            }, length);
        });
    };
})( jQuery );

DEMO.

like image 99
The Alpha Avatar answered Jun 16 '26 19:06

The Alpha



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!