Search code examples
javascriptjqueryruby-on-railsuser-interfacejquery-effects

Why doesn't jQuery delay cause blinking action


jQuery newb here. I want to make a button blink(but not hide). I have this code that I would think work but doesn't:

$("#refresh-60-sec").addClass("pressed-button")
                        .delay(1000)
                        .removeClass("pressed-button")
                        .delay(1000)
                        .addClass("pressed-button")
                        .delay(100)
                        .removeClass("pressed-button")
                        .delay(100)
                        .addClass("pressed-button");

This code just seems to leave the button in the state of the last addClass. Why? and/or how can I achieve the same effect? (That is the brief blinking of an element)

Thanks.


Solution

  • I agree with Kevin I would create blinking function with fades (in and out) on an interval, like this:

    function blink() {
        elem.fadeOut(500, function () {
            elem.fadeIn(500);
        });
    }
    setInterval(blink, 500);
    

    see fiddle for working example, http://jsfiddle.net/tiri/2mkPF/