Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/405.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 运行循环,然后重置并再次运行_Javascript_Loops_Increment - Fatal编程技术网

Javascript 运行循环,然后重置并再次运行

Javascript 运行循环,然后重置并再次运行,javascript,loops,increment,Javascript,Loops,Increment,我想将一个图像更改为图像“I”,直到图像用完,然后我想重新开始 这就是我需要做的 运行以下代码,直到i>=n 然后将i重置为零 代码: 这就是我到目前为止所做的,当循环完成时,我只是对静止I感到困惑: window.setInterval(function(){ advanceSlide(); }, 5000); function advanceSlide(){ while (i<n){ i++; currentIMG='"Im

我想将一个图像更改为图像“I”,直到图像用完,然后我想重新开始

这就是我需要做的

  • 运行以下代码,直到i>=n
  • 然后将i重置为零
代码:

这就是我到目前为止所做的,当循环完成时,我只是对静止I感到困惑:

  window.setInterval(function(){
    advanceSlide();
    }, 5000);

  function advanceSlide(){
    while (i<n){
      i++;
      currentIMG='"Image"+i';
      changeBG(currentIMG);
    }
  };
window.setInterval(函数(){
advanceSlide();
}, 5000);
函数advanceSlide(){

而(i使用全局
imgIndex

imgIndex = 0;
noOfImages = 10;

function advanceSlide(){
  imgIndex++;
  if( imgIndex >= noOfImages ) { imgIndex = 0; }
  currentIMG = "Image" + imgIndex;
  changeBG(currentIMG);
}

下次请把你的问题说清楚

int i = 0;
while (true){
        i++;
        if (i<n){
           currentIMG='"Image"+i';
           changeBG(currentIMG);
        }
        else
        {
           i = 0;
        }
}
inti=0;
while(true){
i++;

如果(iWhen i>=n,那么它将简单地退出循环,并继续执行您在while(){

使用设置的间隔,不需要闭包,因为它只调用另一个函数,可以简化为
window.setInterval(advanceSlide,5000);

您还可以将while循环替换为for循环,因为您只是在增加索引

window.setInterval(advanceSlide, 5000);

function advanceSlide() {
    for(i = 0; i < n; i++) {
        // also here don't really need to store in a variable just pass straight in
        changeBG("Image" + i)
    }
}
window.setInterval(advanceSlide,5000);
函数advanceSlide(){
对于(i=0;i

对于这个答案,我假设你的时间间隔是你想调用函数的方式……这里的另一个答案显示了使用while(1)循环,在没有计时器的情况下,使用另一个循环进行循环。你不需要在一个函数中包装
advanceSlide
。你可以使用模来重置I

window.setInterval(advanceSlide, 5000);
function advanceSlide(){    
    i = (i+1)%n;
    currentIMG="Image"+i;
    changeBG(currentIMG);   
}

因为这是最整洁的答案,即使其他答案被接受。现在我正在删除我的评论,所以不要混淆以后发现这个问题的其他人…对不起
window.setInterval(advanceSlide, 5000);
function advanceSlide(){    
    i = (i+1)%n;
    currentIMG="Image"+i;
    changeBG(currentIMG);   
}