jQuery: infinite loop through array… each()?

前端 未结 4 1491
攒了一身酷
攒了一身酷 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:50

    Array.prototype.recurse = function(callback, delay) {
       delay = delay || 200; 
       var self = this, idx = 0;
    
       setInterval(function() {
          callback(self[idx], idx);
          idx = (idx+1 < self.length) ? idx+1 : 0;
       }, delay);
    }
    

    Try it on StackOverFlow:

    ["#f00", "#ff0", "#f0f", "#f66"].recurse(function(item, index) { 
        $('body').css('background-color', item);
    }, 300);
    

提交回复
热议问题