Guys, I am learning JQuery (and JavaScript) in general.
So I decided to write my first 'Blink' plug in.
It works but can you please take a look an tell me if something is
considered bad practice or can be done better.



(function($) {
    $.fn.blink = function(options) {
        var opts = $.extend({}, $.fn.blink.defaults, options);
        return this.each(function() {
            var $this = $(this);
            var currentColor = opts.color1;

            $this.css({backgroundColor: currentColor});
            window.setInterval(function (){DoTheBlink();},
1000);
            function DoTheBlink()
            {
                if (currentColor == opts.color1)
                    currentColor = opts.color2;
                else
                    currentColor = opts.color1;
                $this.css({ backgroundColor: currentColor });
            }
        });
    };


    $.fn.blink.defaults = {
        color1: 'red',
        color2: 'blue'
    };
})(jQuery);


--------------USE-------------
<div id=t1>hahaha</div>
<div id=t2>hahaha</div>
<div id=t3>hahaha</div>
<script>
    $(document).ready(function() {

    $('#t1').blink({ color1: 'red', color2:'blue' });
    $('#t2').blink({ color1: 'yellow', color2:'black' });
    $('#t3').blink({ color1: 'red', color2:'green' });
});

Thanks
George.

Reply via email to