Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

128
Vistas
how to change image for every 4 seconds in javascript?

i want to change image i have done code still image is not showing at all. file not found error is showing i want to change image every 4 seconds how can i do that? please help

<html lang="en">
<head>
   
    <title>sliding image</title>
</head>
<body onload="f1()">
    <img id="img1" src=""alt="" width="200px">
    <script>
       let arr=new Array();
        function image()
        {  
              let img2=document.getElementById("img1");
              x=0;
              arr[x]="C:\Users\SUDARSHAN\Desktop\html_UI\images\1200px-Heart_corazon.svg.png"
             img2.src=arr[x];
              x=1;
              arr[x]="C:\Users\SUDARSHAN\Desktop\html_UI\images\alex-haney-AGqzy-Uj3s4-unsplash.jpg"
              img2.src=arr[x];
              x=2;
              arr[x]="C:\Users\SUDARSHAN\Desktop\html_UI\images\mitchell-luo-jz4ca36oJ_M-unsplash.jpg"
              img2.src=arr[x];
              x=0;
        }
        function f1()
        {
             window.setInterval(image,4000);
        }
    </script>
</body>
</html>
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

The images have the names 1.jpg, 2.jpg and 3.jpg and are in the same folder with test.html and test.js file. U can switch the timeout in seconds.

<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <meta http-equiv="X-UA-Compatible" content="IE=edge">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <title>Document</title>
   <script src="test.js" type="text/javascript" defer></script>
</head>
<body>
   <img id="imageSwitcher" src="" alt="">
</body>
</html>
"use strict";

const imageArray = ["1.jpg", "2.jpg", "3.jpg"];
const imageSwitcher = document.getElementById("imageSwitcher");

let timeout = 4;
let i = 0;
setInterval(() => {
   imageSwitcher.src=imageArray[i];
   console.log(i);
   i++;
   if (i >= imageArray.length) {
      i = 0;
   }
}, timeout * 1000);
about 4 years ago · Juan Pablo Isaza Denunciar

0

You have to increment the pointer and reset it like below code.

<script>

    var pointer = 0;

    var imageArray = [
        "C:\Users\SUDARSHAN\Desktop\html_UI\images\1200px-Heart_corazon.svg.png",
        "C:\Users\SUDARSHAN\Desktop\html_UI\images\alex-haney-AGqzy-Uj3s4-unsplash.jpg",
        "C:\Users\SUDARSHAN\Desktop\html_UI\images\mitchell-luo-jz4ca36oJ_M-unsplash.jpg"
    ];


    function image()
    {  
      
          let img2 = document.getElementById("img1");
          
          img2.src = imageArray[pointer];

          pointer++;

          // reset the pointer if hit the max
          if (pointer == imageArray.length-1){
            pointer = 0;
          }

    }
    function f1()
    {
         window.setInterval(image,4000);
    }
</script>
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda