0

如果我"body onload="varname=setInterval etc, etc""最终同时setInterval运行两个事件,如果我得到"overload == true"它并不会重置ketchCount并且它不会更新数组中的图片,它只会启动setInterval并从那里开始工作。我知道我很新,但我会很感激任何帮助!

ketchupArray=new Array();
ketchupArray[0] = new Image(192, 168);
ketchupArray[0].src = "./pictures/levels/nothing.jpg?";
ketchupArray[1] = new Image(192, 168);
ketchupArray[1].src = "./pictures/levels/lowest.jpg?";
ketchupArray[2] = new Image(192, 168);
ketchupArray[2].src = "./pictures/levels/low.jpg?";
ketchupArray[3] = new Image(192, 168);
ketchupArray[3].src = "./pictures/levels/medium.jpg?";
ketchupArray[4] = new Image(192, 168);
ketchupArray[4].src = "./pictures/levels/high.jpg?";
ketchupArray[5] = new Image(192, 168);
ketchupArray[5].src = "./pictures/levels/highest.jpg?";

overload = false;
ketchCount = 0;
var lessCount;
var overCount;
var lessCountVar;

function overFunction()
{
overload == false;
ketchCount == 0;
document.getElementById("ketchupLevel").src = ketchupArray[ketchCount].src;
lessCountVar=setInterval(function(){lessCount()},5000);
}

function lessCount()
{
if (ketchCount == 0)
  {
  return;
  }
ketchCount--;
document.getElementById("ketchupLevel").src = ketchupArray[ketchCount].src;
}

function moreKetchup()
{
if (overload == true) 
  {
  return;
  }
ketchCount++;
document.getElementById("ketchupLevel").src = ketchupArray[ketchCount].src;
if (ketchCount == 5)
  {
  overload == true;
  clearInterval(lessCountVar);
  overCount=setTimeout(function(){overFunction()},5000);
  }
}

function lessKetchup()
{
if (overload == true)
  {
  return;
  }
else if (ketchCount == 0)
  {
  return;
  }
ketchCount--;
document.getElementById("ketchupLevel").src = ketchupArray[ketchCount].src;
}
4

1 回答 1

0

您可以尝试两件事:

onload使用中首次启动setTimeout()overFunction()

overFunction()您的代码中可能有错误。您正在比较重载和 ketchCount,而不是为它们分配值。正确的代码应该看起来更像

function overFunction()
{
  overload = false;
  ketchCount = 0;
  document.getElementById("ketchupLevel").src = ketchupArray[ketchCount].src;
  lessCountVar=setInterval(function(){lessCount()},5000);
}
于 2012-08-13T12:56:15.123 回答