最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Change background color with a loop onclick - Stack Overflow

programmeradmin1浏览0评论

here is my js fiddle : /

 var box = document.getElementById('box');

 var colors = ['purple', 'yellow', 'orange', 'brown', 'black'];

 box.onclick = function () {

    for (i = 0; i < colors.length; i++) {

        box.style.backgroundColor = colors[i];


      }

};

I'm in the process of learning JavaScript. I was trying to get this to loop through each color in the array, but when i click the box (demonstration on jsfiddle) it goes to the last element in the array.

here is my js fiddle : http://jsfiddle/pYM38/16/

 var box = document.getElementById('box');

 var colors = ['purple', 'yellow', 'orange', 'brown', 'black'];

 box.onclick = function () {

    for (i = 0; i < colors.length; i++) {

        box.style.backgroundColor = colors[i];


      }

};

I'm in the process of learning JavaScript. I was trying to get this to loop through each color in the array, but when i click the box (demonstration on jsfiddle) it goes to the last element in the array.

Share Improve this question asked May 22, 2014 at 23:24 HobbesHobbes 1063 gold badges3 silver badges10 bronze badges 7
  • It does loop through the array...and when it's finished it ends on the last one so color = 'black'. edit: i think loops happen faster then you think. – 13ruce1337 Commented May 22, 2014 at 23:27
  • 2 jsfiddle/pYM38/17 – Jared Farrish Commented May 22, 2014 at 23:29
  • @JaredFarrish this is a stack. <3 Note: you must click multiple times for this to work. – 13ruce1337 Commented May 22, 2014 at 23:29
  • @13ruce1337 - That can be interpreted multiple ways. ;) – Jared Farrish Commented May 22, 2014 at 23:30
  • @JaredFarrish please post this as an answer. this is clear and simple. Luke no discredit, great answer as the question was unclear about what he wanted to do in the first place. – 13ruce1337 Commented May 22, 2014 at 23:32
 |  Show 2 more ments

2 Answers 2

Reset to default 2

Here are two methods, depending on what you're up to:

Loop to Next on Click

var box = document.getElementById('box'),
    colors = ['purple', 'yellow', 'orange', 'brown', 'black'];

box.onclick = function () {
    color = colors.shift();
    colors.push(color);

    box.style.backgroundColor = color;
};

http://jsfiddle/pYM38/17/

Loop Through All on Click

var box = document.getElementById('box'),
    colors = ['purple', 'yellow', 'orange', 'brown', 'black'];

box.onclick = function () {
    (function loop(){
        var color = colors.shift();

        box.style.backgroundColor = color;

        if (colors.length) {
            setTimeout(loop, 1000);
        }
    })();
};

http://jsfiddle/pYM38/23/

Restarts on Next Click

var box = document.getElementById('box'),
    colors = ['purple', 'yellow', 'orange', 'brown', 'black'];

box.onclick = function () {
    var set = colors.slice(0);

    (function loop(){
        var color = set.shift();

        box.style.backgroundColor = color;

        if (set.length) {
            setTimeout(loop, 1000);
        }
    })();
};

you want it to be animated, or delayed?

Because your example works perfectly, you are looping through all colors and it is so fast that you see only the last one.

var box = document.getElementById('box');

 var colors = ['purple', 'yellow', 'orange', 'brown', 'black'];
 var running = 0;    
 box.onclick = function () {
    if(running>0) return;
    for (i = 0; i < colors.length; i++) {
        running++;
        setTimeout(function() {
             box.style.backgroundColor = colors[i];
             running--;
        }, 1000);

      }

};
发布评论

评论列表(0)

  1. 暂无评论