Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

207
Views
¿Por qué las imágenes no se cambian cada 2 segundos?
let i = 0; function change() { if (i < res.length) document.getElementById("cont").style.backgroundImage = `url(static/letters/${res[i++]}.jpg)`; } window.onload = function() { setInterval(change, 2000); };

Quiero este cambio solo una vez hasta que se muestren todas las imágenes y luego quiero que el contenedor esté vacío. ¿Qué pasa con mi código?

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Puedes comprobar esta implementación.

 let i = 0; let interval //the variable to keep the current interval //simulate your `res` data const res = [ "https://cdn.pixabay.com/photo/2015/04/23/22/00/tree-736885__480.jpg", "https://images.unsplash.com/photo-1612151855475-877969f4a6cc?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxzZWFyY2h8MXx8aGQlMjBpbWFnZXxlbnwwfHwwfHw%3D&w=1000&q=80", "https://www.w3schools.com/w3css/img_lights.jpg" ] function change() { if (i < res.length) { document.getElementById("cont").style.backgroundImage = `url(${res[i++]})`; } else { document.getElementById("cont").style.backgroundImage = "none"; clearInterval(interval); //remove the current interval after finish the loop i = 0; //reset index value } } window.onload = function() { interval = setInterval(change, 2000); //set the current interval };
 #cont { height: 300px; width: 300px; background-position: center; background-repeat: no-repeat; background-size: 300px 300px; transition: 0.2s; }
 <div id="cont"></div>

about 4 years ago · Santiago Trujillo Report

0

Esto podría hacer el trabajo:

 if (i < res.length) document.getElementById("cont").style.backgroundImage = `url('static/letters/${res[i++]}.jpg')`;
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!