jQuery: infinite loop through array… each()?

前端 未结 4 1531
攒了一身酷
攒了一身酷 2021-01-06 06:16

Fiddle here: http://jsfiddle.net/F6nJu/

I have a colored box:

#colorblock { background:#3ff; width: 100%; h
4条回答
  •  被撕碎了的回忆
    2021-01-06 06:53

    Try this:

    var arr = ["#f00", "#ff0", "#f0f", "#f66"];
    $.each(arr, function(key, value) {
        set_color(value);
    });
    
    function set_color(color) {
        $('#colorblock').delay('1200').animate({
            backgroundColor: color
        }, 600);
        setTimeout(function() {
            set_color(color);
        }, 2000); //restart in 2 seconds
    }
    

    Fiddle: http://jsfiddle.net/maniator/F6nJu/1/

提交回复
热议问题