简体   繁体   中英

Global variable doesn't change with jquery function

I have a problem with my code. So I am trying to change the value of interval from 9 to the value of current_interval. This happens via a click event. But when the ChangePicture() function starts interval is still 9 (basically i have a picture rotator script that changes picture after 5 seconds).

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;
}

You should trying using a callback :

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM