0

我的代码有问题。所以我试图将间隔的值从 9 更改为 current_interval 的值。这通过点击事件发生。但是当 ChangePicture() 函数开始时,间隔仍然是 9(基本上我有一个图片旋转器脚本,可以在 5 秒后更改图片)。

var interval = 9;
var globalCounter = 0;
var temp = setInterval(function() {ChangePicture()}, 5000);


function SetInterval(i)
{
    interval = i;
}

$(document).on("click", ".picture_button_lol", function(event){
    var current_interval    = $(this).children(".amount_of_pictures").val();
    SetInterval(current_interval);

});

function ChangePicture()
{
    var pictureId = globalCounter%interval;
    ShowAndHide(pictureId);
    globalCounter = globalCounter + 1;
}

function ShowAndHide(picId)
{
    var picture = "bigview_" + picId;
    var text    = "bigview_text_" + picId;
    var button  = "bigview_button_" + picId;
    var z_index = 1;

    document.getElementById(picture).style.zIndex   = z_index;
    document.getElementById(text).style.zIndex      = z_index;
    document.getElementById(button).style.backgroundColor = "#ffffff";
    document.write(interval);
    for(var i=0; i<interval; i++)
    {
        if(i != picId)
        {
            picture = "bigview_" + i;
            text    = "bigview_text_" + i;
            button  = "bigview_button_" + i;
            z_index = 0;

            document.getElementById(picture).style.zIndex   = z_index;
            document.getElementById(text).style.zIndex      = z_index;
            document.getElementById(button).style.backgroundColor = "#000000";
        }
    }

    globalCounter = picId;
}
4

1 回答 1

0

您应该尝试使用回调:

function ChangePicture()
{
    var pictureId = globalCounter%interval;
    ShowAndHide(pictureId, function() {
        globalCounter = globalCounter + 1;
    });    
}

function ShowAndHide(picId, callback)
{
    var picture = "bigview_" + picId;
    var text    = "bigview_text_" + picId;
    var button  = "bigview_button_" + picId;
    var z_index = 1;

    document.getElementById(picture).style.zIndex   = z_index;
    document.getElementById(text).style.zIndex      = z_index;
    document.getElementById(button).style.backgroundColor = "#ffffff";
    document.write(interval);
    for(var i=0; i<interval; i++)
    {
        if(i != picId)
        {
            picture = "bigview_" + i;
            text    = "bigview_text_" + i;
            button  = "bigview_button_" + i;
            z_index = 0;

            document.getElementById(picture).style.zIndex   = z_index;
            document.getElementById(text).style.zIndex      = z_index;
            document.getElementById(button).style.backgroundColor = "#000000";
        }
    }

    globalCounter = picId;
    callback();
}
于 2013-08-29T09:43:25.867 回答