简体   繁体   English

JavaScript-重复功能以显示图像

[英]JavaScript- repeat function to display images

I have the following 2 JS function's. 我有以下2个JS函数。 Once a user enters a key the image on screen changes. 用户输入键后,屏幕上的图像就会更改。 I currently have 8 images which I am working with. 我目前正在使用8张图片。 How can I make this repeat itself so that the user will have to go through the 8 same images again? 我如何使自己重复一次,以便用户将不得不再次浏览8张相同的图像?

function start(){
    if (running==0){
        running = 1;       
        initialTime = Date.now();
    }
    else{
        timer1;
        imagecounter++;
        reset();
    }
}

   function reset(){

    time = 0;

    if(imagecounter > 8){
        document.getElementById("imageDisplay").src ="images/image.png"; 
    }
    else 
    {
        running = 0;
        initialTime = Date.now();
        document.getElementById("imageDisplay").src ="images/image"+imagecounter+".png";
        start();
    }
}

when you increase imagecounter, first check to see if it's greater than or equal to the length of your image array -1. 当增加imagecounter时,首先检查它是否大于或等于图像数组-1的长度。 if it is, then set it to 0. 如果是,则将其设置为0。

Your code is a little difficult to read but I think what you want to do is reset imagecounter to 0 so that it starts over. 您的代码有点难以阅读,但我认为您想做的是将imagecounter重置为0,以便重新开始。

Change your reset function: 更改您的reset功能:

function reset(){

    time = 0;

    if(imagecounter > 8){
        imagecounter = 0; // <-- reset to 0
        document.getElementById("imageDisplay").src ="images/image.png"; 
    }
    else 
    {
        running = 0;
        initialTime = Date.now();
        document.getElementById("imageDisplay").src ="images/image"+imagecounter+".png";
        start();
    }
}

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

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