1

我之前就如何使用我的 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);
}
4

3 回答 3

2

正如评论中所指出的,您没有在最后一次迭代中更改背景,这就是为什么它比其他人更长时间地徘徊,这应该保持一致

工作示例: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);
于 2013-09-10T17:47:53.723 回答
0
$('body').css('background', Backgrounds[Background_Stage++]);

相当于

$('body').css('background', Backgrounds[Background_Stage]);
Background_Stage = Background_Stage + 1;

每次循环时,背景都会设置为未定义。如果你想在被访问之前有你的变量增量,你需要使用

$('body').css('background', Backgrounds[++Background_Stage]);

完整示例(如果条件也被修改):

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 - 1) Background_Stage = -1;
            $('body').css('background', Backgrounds[++Background_Stage]); 
    }, 1000);
}
于 2013-09-10T19:39:44.247 回答
0

我建议不要进行 -1 初始化,而是使用模块。即3 % 2 == 1产生除法的其余部分。

http://jsfiddle.net/KtsHa/1/

(function Background () {
    var Backgrounds = [
        '#333', '#777', '#999', '#CCC'
    ],
    Max_Backgrounds = Backgrounds.length, 
    Background_Stage = 0,
    changeBackground = function () {
        $('body').css('background', Backgrounds[Background_Stage]); 
        Background_Stage = (Background_Stage + 1) % Max_Backgrounds;                
    };
    changeBackground(); // Remove this line if what you intend is to wait one second before the first change
    setInterval(changeBackground, 1000);
}());

此外,已知 setInterval 会漂移,所以我宁愿使用 setTimeout 并在 1 秒后再次执行:http: //jsfiddle.net/KtsHa/2/

(function () {
    var Backgrounds = [
        '#333', '#777', '#999', '#CCC'
    ],
    Max_Backgrounds = Backgrounds.length, 
    Background_Stage = 0,
    changeBackground = function () {
        $('body').css('background', Backgrounds[Background_Stage]); 
        Background_Stage = (Background_Stage + 1) % Max_Backgrounds;
        setTimeout(changeBackground, 1000);
    };
    changeBackground();
}());
于 2013-09-10T19:27:41.493 回答