我之前曾寻求过有关如何使用JavaScript代码修复问题的帮助,现在已经修复了问题,但是我的代码有问题吗?由于最后一个阵列的持续时间超过1秒,因此它会持续约5秒钟。这是因为我要重置数组还是Javascript中的正常现象?
function Background()
{
var Backgrounds = [
'#333', '#777', '#999', '#CCC'
],
Max_Backgrounds = Backgrounds.length,
Background_Stage = -1;// Yes, it's meant to be -1 so when background_stage++; is called it will make it 0 thus start at the beginning of the array.
setInterval(function() {
if(Background_Stage >= Max_Backgrounds) Background_Stage = -1;
$('body').css('background', Backgrounds[Background_Stage++]);
}, 1000);
}
最佳答案
正如评论中指出的那样,您不会在上一次迭代中更改背景,这就是为什么它会比其他背景停留更长的时间,因此应该保持一致
工作示例:http://jsfiddle.net/uRSC5/
function background() {
var backgrounds = [
/*"./../../Styles/Callum_Project1/Images/Background_1",
"./../../Styles/Callum_Project1/Images/Background_2",
"./../../Styles/Callum_Project1/Images/Background_3"*/
'#333', '#777', '#999', '#CCC'
];
var count = backgrounds.length;
var i = 0;
setInterval(function() {
$('body').css('background', backgrounds[i++]);
if(i >= count) i = 0;
}, 1000);
}
$(background);