[英]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.
所以我试图将时间间隔的值从9更改为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).
但是,当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;
}
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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.