0

Here is the code.

HTML

    <body>
        <h1>111</h1>
        <h1>222</h1>
        <h1>333</h1>
        <h1>444</h1>
    </body>

JS

$(function(){
    blink("h1", 100);
});

(function(){
    $.fn.myBlink = function(speed){
        return this.each(function(){
            //setInterval(function(){
                $(this).fadeOut(speed).fadeIn(speed);
            //}, speed);
        });
    }
}(jQuery));

function blink(selector, speed){
    setInterval(function(){
        $(selector).myBlink(speed);
    }, speed);
}

This is working perfectly. But I want to combine function blink and plugin myBlink together. If release the comment code, it doesn't work. What am I done wrong? $(function(){ blink("h1", 100); });

(function(){ $.fn.myBlink = function(speed){ return this.each(function(){ //setInterval(function(){ $(this).fadeOut(speed).fadeIn(speed); //}, speed); }); } }(jQuery));

Yes, I find out the answer!

function blink(selector, speed){
    setInterval(function(){
        $(selector).myBlink(speed);
    }.bind(this), speed);
}
SPG
  • 6,109
  • 14
  • 48
  • 79

1 Answers1

0

try to put the return statement into setInterval. It should works

Marco Mercuri
  • 1,117
  • 9
  • 17