简体   繁体   English

我想重复代码,但不知道如何使用循环

[英]I want to repeat code but don't know how t use loops

I have this code: 我有以下代码:

js: js:

function change_color(color) {
$("body").animate({ backgroundColor:color }, '1000');
}

setTimeout(function () {
    change_color('#4AC900'
    );
}, 500);
setTimeout(function () {
    change_color('#964514'
    );
}, 1500);
setTimeout(function () {
    change_color('#EE0000'
    );
}, 1500);
setTimeout(function () {
    change_color('#FFE303'
    );
}, 1500);
setTimeout(function () {
    change_color('#8E388E'
    );
}, 1500);
setTimeout(function () {
    change_color('#FF00AA'
    );
}, 1500);

and I want to use it repeatedly but putting it in a while loop just crashes the site can anyone help? 并且我想重复使用它,但是将其置于while循环中只会使网站崩溃,任何人都可以帮忙吗?

Here is the site... its my little brothers site not mine... http://timothy.techbytbone.com/isaac.php 这是网站...它是我的弟弟网站而不是我的网站... http://timothy.techbytbone.com/isaac.php

var colors = ['#4AC900', '#964514', '#EE0000', '#FFE303', '#8E388E', '#FF00AA'],
    len = colors.length,
    i;

for (i = 0; i < len; i++) {
    (function(i, color) {
        setTimeout(function () {
            change_color(color);
        }, (i + 1) * 500);
    })(i, colors[i]);
}

this is all you need: 这就是您所需要的:

jsFiddle demo jsFiddle演示

var c = 0;
var colors = ['#4AC900','#964514','#EE0000','#FFE303','#8E388E','#FF00AA'];
(function loop(){         
    $('body').stop().animate({backgroundColor : colors[c++%colors.length] }, 1000, loop);  
})();

(Prest attention that you need to use the jQuery UI to animate the CSS background-color property) (请注意,您需要使用jQuery UI来为CSS background-color属性设置动画)

var colors = {'#4AC900': 500,
              '#964514': 1500,
              // etc. Just continue with the color-millisecond combinations
             }

for(key in colors) {
 setTimeout(function () {
    change_color(key);
 }, colors[key]);
}

Your loop is crashing because you can't set all the necessary timeouts at browser's loading. 由于无法在浏览器加载时设置所有必需的超时,因此循环崩溃。 Here is a version of your code that should work. 这是应该工作的代码版本。

var colors = ['#4AC900', '#964514', '#EE0000', '#FFE303', '#8E388E', '#FF00AA'];

var currentColorIndex = 0;
var scheduleChange;
scheduleChange = function() {
    change_color(currentColorIndex);
    currentColorIndex = (currentColorIndex + 1) % colors.length
    setTimeout(scheduleChange, 1000);
};

setTimeout(scheduleChange, 500);
function change_color(color) {
  $("body").animate({ backgroundColor:color }, '1000');
}

setTimeout(function() {
  change_color('#4AC900')
}, 500);

colors = ['#964514', '#EE0000', '#FFE303', '#8E388E', '#FF00AA']

interval = setInterval(function() {
  if (! a.length) {
    return clearInterval(interval);
  }
  change_colors(a.shift());
}, 1500);

Have Fun. 玩得开心。 You should learn about closures for not messing setIntervals. 您应该了解不使setIntervals混乱的闭包 There are tons of libraries that animate colors and other stuff. 有大量的库可以对颜色和其他内容进行动画处理。 I can recommend morpheus by ded . 我可以通过ded推荐语素

暂无
暂无

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

相关问题 我想使用DataTables列搜索。 但是我不知道服务器端代码应该如何 - I want to use DataTables column search. But I don't know how should be server side code 想在我的代码中使用 for 循环,但我不知道怎么可能 - A want to use the for loop in my code, but I don't know how is it possible 我不想在javascript上重复相同的代码**新** - I don't want to repeat the same code on javascript **new ** 我如何不重复部分代码 - How can I don't repeat a part of code 不知道如何在代码中使用 target=&quot;_blank&quot; - Don't know how to use target="_blank" in code 我是 React 的新手,我想将普通的 javascript 代码转换为 React,我不知道该怎么做 - I am new in react i want to convert normal javascript code to the react i don't know how to do that 我知道我想要什么,但我不知道该怎么做! (chrome扩展名) - I know what I want, but I don't know how to do it! (chrome extension) 数据添加到firestore的时候,想用onSnapshot监听,这样就可以自动调用数据了,但是不知道怎么用 - When data is added to the firestore, I want to listen with onSnapshot so that the data can be called automatically, but I don't know how to use 在这种情况下如何使用泛型这种类型? 我不知道为什么 - how to use generic this type in this case ? i don't know why 我不知道如何在 sequelize (Mac) 中使用 POST 方法 - I don't know how to use POST method in sequelize (Mac)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM