最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - change image on timer, - Stack Overflow

matteradmin8PV0评论

I know this question is asked frequently but I can not copy a code. I want the image to change after a time. I think I made a lot of wrong coding errors. Below is my code:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>test</title>
        <link rel="stylesheet" type="text/css" href="/css/styles.css">
    </head>
    <body>
        <img id="image" src="foto1.jpg">

        <script type = "text/javascript">
            var image=document.getElementById("image");

            function volgendefoto() {
                if (images==0) {
                    image.src="foto2";
                }

                if (images==1) {
                    image.src="foto2";
                }
                if (images==3) {
                    image.src="foto1";
                }
            }

            function timer(){
                setInterval(volgendefoto, 3000);
            }

            var images= [], x = -1;
            image[0]="foto1.jpg";
            image[1]="foto2.jpg";
            image[2]="foto3.jpg";
        </script>
    </body>
</html>

Thanks in advance for help,

Jasper

I know this question is asked frequently but I can not copy a code. I want the image to change after a time. I think I made a lot of wrong coding errors. Below is my code:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>test</title>
        <link rel="stylesheet" type="text/css" href="/css/styles.css">
    </head>
    <body>
        <img id="image" src="foto1.jpg">

        <script type = "text/javascript">
            var image=document.getElementById("image");

            function volgendefoto() {
                if (images==0) {
                    image.src="foto2";
                }

                if (images==1) {
                    image.src="foto2";
                }
                if (images==3) {
                    image.src="foto1";
                }
            }

            function timer(){
                setInterval(volgendefoto, 3000);
            }

            var images= [], x = -1;
            image[0]="foto1.jpg";
            image[1]="foto2.jpg";
            image[2]="foto3.jpg";
        </script>
    </body>
</html>

Thanks in advance for help,

Jasper

Share Improve this question edited Jul 29, 2018 at 21:03 PoLáKoSz 3571 gold badge6 silver badges9 bronze badges asked Dec 2, 2016 at 10:10 user7104802user7104802 3
  • "cannot copy code"? sounds like a school assignment. back in my days we knew how to copypaste and change variable names ;) – xShirase Commented Dec 2, 2016 at 10:11
  • yeah, it is for school. it is too obvious if i copy it, they know my coding behaviour and mon mistakes :) – user7104802 Commented Dec 2, 2016 at 10:12
  • @xShirase, haha ikr – AzizurRahamanCA Commented Dec 2, 2016 at 10:13
Add a ment  | 

2 Answers 2

Reset to default 6
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>test</title>
        <link rel="stylesheet" type="text/css" href="/css/styles.css">
    </head>

    <body>
        <img id="image" src="foto1.jpg">

        <script type = "text/javascript">
            var image = document.getElementById("image");
            var currentPos = 0;
            var images = ["foto1.jpg", "foto2.jpg", "foto3.jpg"]

            function volgendefoto() {
                if (++currentPos >= images.length)
                    currentPos = 0;

                image.src = images[currentPos];
            }

            setInterval(volgendefoto, 3000);
        </script>
    </body>
</html>

You're not creating your interval, timer is never executed. Here's one that will loop your pictures, assuming that your first image is images is the one preloaded (eg: images[0] === image.src on page load) :

  const image=document.getElementById("image");
  let images= ["foto1.jpg","foto2.jpg","foto3.jpg"], i = 0;

  function volgendefoto() {
      i<images.length?i+=1:i=0; 
      image.src=images[i];
  }
  setInterval(volgendefoto, 3000);

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far