简体   繁体   English

为什么不循环?

[英]Why won't this loop?

I have written a javascript program that changes the picture on a header, waits, then shows the next one. 我编写了一个javascript程序,该程序可以更改标题上的图片,等待,然后显示下一个。

I have tried to make it loop round back to 1 when it reaches the last picture, but it's not working, the computer keeps crashing and I'm really stuck. 我试图使它在到达最后一张图片时循环回到1,但是它无法正常工作,计算机不断崩溃,我真的被卡住了。

Later on it's going to have options to fade and have different transitions, but right now, I can't even get the function to loop forever without crashing the computer. 稍后它将具有淡入淡出和不同过渡效果的选项,但是现在,我什至无法在不使计算机崩溃的情况下获得永久循环的功能。

Can anyone offer a solution? 谁能提供解决方案?

Thank you, 谢谢,

Andrew 安德鲁

var $initialDelay = 0; 
var $delay = 200;
var $picture = 1;
var $pictureAmount = 4;

function myFunction() {
    //first picture loaded in CSS
    //loop through pictures
    for (i=0;i<$pictureAmount;i++) {
        setTimeout(function(){  
            document.getElementById("header").style.backgroundImage = "url(img/slideshow/slideshow"+$picture+".jpg)";
            $picture++;
        }, $initialDelay);
        $initialDelay += $delay;
    }
}
myFunction();

Here is an example for looping over a 4 images with a timeout. 这是一个有超时循环播放4张图像的示例。 I think you should be able to use this in your code to do what you want. 我认为您应该可以在代码中使用此功能来执行所需的操作。

 const delayBetweenPictures = 1000, numberOfPictures = 4, initialPicture = 1, image = document.getElementById('slideshow'); function changeToPicture(pictureIndex) { console.log(`Changing picture to index ${pictureIndex}`); // Change the image image.src = `http://lorempixel.com/320/200/cats/${pictureIndex}`; // Use a modulo operator to turn 4 (number of pictures) back to 0 and add 1 so the range becomes 1...number of pictures. pictureIndex = (pictureIndex % numberOfPictures) + 1; // Set a timeout of X ms after which the changeToPicture method is called again, this time with the new value of pictureIndex. setTimeout((newIndex) => changeToPicture(newIndex), delayBetweenPictures, [pictureIndex]); } changeToPicture(initialPicture); 
 <img id="slideshow"> 

If you want this to continuously change picture every 200ms 如果您希望每200ms连续更改一次图片

var delay = 200;
var picture = 1;
var pictureAmount = 4;

function myFunction() {
    setTimeout(function() {
        document.getElementById("header").style.backgroundImage = "url(img/slideshow/slideshow" + picture + ".jpg)";
        picture = (picture % pictureAmount) + 1;
        myFunction();
    }, delay);
}
myFunction();

By using the modulo operation you can loop between the values 1 to 4 over and over. 通过使用模运算,您可以反复在值1到4之间循环。

It would be easier if your pictures were "0-indexed" : 如果您的图片被“ 0索引”会更容易:

for (i=0; /* infinite loop */; i=(i+1)%$pictureAmount) {
    // loops through 0, 1, ..., $pictureAmount - 1
}

but can be adjusted for a 1-indexed iteration : 但可以针对1索引的迭代进行调整:

for (i=1; /* infinite loop */; i=(i%$pictureAmount)+1) {
    // loops through 1, 2, ..., $pictureAmount
}

Example iteration over 1 to 9 : 示例迭代1到9:

 function sleep(ms) { return new Promise(resolve => setTimeout(resolve, ms)); } async function loop() { for (i=1; /* infinite loop */; i=(i%9)+1) { document.getElementById("view").innerHTML=i; await sleep(1000); } } loop(); 
 <p id="view"></p> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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