JavaScript - setInterval / setTimeout& clearInterval,不会正常执行

时间:2012-08-13 12:50:52

标签: javascript settimeout setinterval

如果我"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;
}

1 个答案:

答案 0 :(得分:0)

您可以尝试两件事:

<{1>} onload使用setTimeout()首次启动overFunction()

overFunction()

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

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