我想每隔60秒执行一个不同的动作,通过动画来改变我的背景。现在单击即可运行。
$(document).ready(function(){
$("li.one").click( function(){
$('#switch-container').animate({backgroundColor: '#18597f'}, 1000)
});
$("li.two").click( function(){
$('#switch-container').animate({backgroundColor: '#8a0651'}, 1000)
});
$("li.three").click( function(){
$('#switch-container').animate({backgroundColor: '#8a0651'}, 1000)
});我怎么能这样做呢?谢谢!
发布于 2013-03-20 16:12:24
var colors = ['#18597f','#8a0651','#8a0651'],
timer = setInterval(function() {
var rand = parseInt(Math.random()*(3 - 0),10);
$('#switch-container').animate({backgroundColor: colors[rand]}, 1000);
}, 1000);编辑:
要按常规顺序而不是随机更改颜色,请执行以下操作:
var colors = ['green','red','yellow'],
i = 0,
timer = setInterval(function() {
$('#switch-container').animate({backgroundColor: colors[i++]}, 500);
i = i==3 ? 0 : i;
}, 1000);发布于 2013-03-20 16:08:11
使用setInterval()。
setInterval()方法以指定的间隔(毫秒)调用函数或计算表达式。
setInterval(function(){
//code for animation
},DURATION);发布于 2013-03-20 16:11:49
$(document).ready(function () {
// alert("hello");
changecolor();
});
function changecolor() {
// alert("hi");
var colors = ["#00FF00", "#CCCCCC", "#990099", "#FEA400", "#FF9900", "#6600FF", "#333333", ];
var rand = Math.floor(Math.random() * colors.length);
$('#controls-wrapper').css("background-color", colors[rand]);
setTimeout('changecolor()', 100);
}如果您不关心计时器中的代码是否会比您的时间间隔更长,请使用setInterval():
setInterval(function, delay)它一遍又一遍地触发作为第一个参数传入的函数。
一种更好的方法是,将setTimeout与自动执行的匿名函数一起使用:
(function(){
// do some stuff
setTimeout(arguments.callee, 60000);
})();这保证了在代码执行之前不会进行下一次调用。在本例中,我使用arguments.callee作为函数引用。给函数命名并在setTimeout中调用是一种更好的方式,因为arguments.callee在ecmascript 5中已被弃用。
https://stackoverflow.com/questions/15518240
复制相似问题