简体   繁体   English

JavaScript foreach循环遍历十六进制值数组,使用setTimeout循环遍历背景颜色

[英]JavaScript foreach loop through an array of hex values, using setTimeout to loop through background colors

I've been trying to come up with the most concise way that I can possibly change background colors using JavaScript. 我一直在努力想出最简洁的方法,我可以用JavaScript改变背景颜色。 (Trying to get the hang of forEach and higher order functions just for fun.) Anyway, this will run on page load, and I think I'm pretty close: (试图获取forEach和更高阶函数的悬念只是为了好玩。)无论如何,这将在页面加载时运行,我认为我非常接近:

function background(){
    var colorArray = ["#14183b", "#002e2e", "#0d2d40", "#173052", "#194759", "#296b73"];
    function change(newcolor){
        document.body.style.backgroundColor=newcolor;
    }
    colorArray.forEach(function(color){
        setTimeout(change(color), 1000);
    });
}

The problem is that the background color is only showing the last element in the array. 问题是背景颜色只显示数组中的最后一个元素。 I also am not sure how to start the forEach loop over again when it is has finished. 我也不确定如何在完成后再次启动forEach循环。 Thanks for any help! 谢谢你的帮助!

Like this ... Also can ... 像这样...也可以......

var colorArray = ["#14183b", "#002e2e", "#0d2d40", "#173052", "#194759", "#296b73"]; var colorArray = [“#14183b”,“#002e2e”,“#0d2d40”,“#173052”,“#194759”,“#296b73”];

var count = 0; var count = 0;

    function change() {
        document.body.style.backgroundColor = colorArray[count];
        count++;
        if(count == colorArray.length) {
            count = 0;
        }
    }

    setInterval(function(){
        change();
    }, 1000);

The problem is you are passing the value returned by change as the timeout handler, instead you need to pass a function reference. 问题是您将change返回的值作为超时处理程序传递,而是需要传递函数引用。

If you want to have an infinite loop, then you can use setInterval() like 如果你想拥有一个无限循环,那么你可以使用setInterval()类的

 function background() { var colorArray = ["#14183b", "#002e2e", "#0d2d40", "#173052", "#194759", "#296b73"]; function change() { index = index >= colorArray.length ? 0 : index; document.body.style.backgroundColor = colorArray[index++]; } var index = 0; var interval = setInterval(change, 1000); } background(); 


If you want to use setTimeout() itself and want to iterate only once then you can use 如果您想使用setTimeout()本身并且只想迭代一次,那么您可以使用

 function background() { var colorArray = ["#14183b", "#002e2e", "#0d2d40", "#173052", "#194759", "#296b73"]; function change(newcolor) { document.body.style.backgroundColor = newcolor; } colorArray.forEach(function(color, i) { setTimeout(change.bind(undefined, color), i * 1000); }); } background(); 

Improving .... 改善....

var colorArray = ["#14183b", "#002e2e", "#0d2d40", "#173052", "#194759", "#296b73"];

 var count = 0;

setInterval(function(){
    document.body.style.backgroundColor = colorArray[count];
    count++;
    if(count == colorArray.length) {
        count = 0;
    }
}, 1000);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM