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

119
Views
Agregue imágenes al DOM después de cada 2 segundos

Estoy haciendo un sitio web de feliz cumpleaños para un amigo. Quiero agregar imágenes al DOM después de cada 2 segundos en coordenadas específicas.

Tengo una función setTimeout en mi script de la siguiente manera:

 setTimeout(function(){ createBalloon(); }, 2000);

La función de creación de globos es la siguiente:

 function createBalloon() { var colors = ["10vw","30vw","50vw","70vw", "90vw"]; var randColor = colors[Math.floor(Math.random() * colors.length)]; var img = document.createElement("img"); img.src = "balloon.jpg"; img.className = "balloon"; var div = document.getElementById("balloon-div"); div.appendChild(img); img.style.position = "absolute"; img.style.top = "100vh"; img.style.left = randColor; var balloonList = document.getElementsByClassName("balloon"); }

Tengo todo listo... pero cuando pongo setTimeout en un bucle infinito, evita que la página se cargue.

alguna pista de como hacer la tarea????

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Use la función setInterval en su lugar, activará cada período de tiempo.

 setInterval(function () { createBalloon(); }, 2000);
about 4 years ago · Juan Pablo Isaza Report

0

La función setInterval es la mejor manera de hacer la tarea... pero una alternativa es la recursividad:

 function loop(){ createBalloon(); setTimeout(function(){ loop(); }, 2000); } loop(); // make sure it's the last line in your script
about 4 years ago · Juan Pablo Isaza 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!