Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

127
Visualizações
Add images to the DOM after every 2 seconds

I am making a happy birthday website for a friend. I want to add images to the DOM after every 2s at a specific co-ordinates.

I have a setTimeout function in my script as follows :

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

The create balloon function is as follows :

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");
}

I have all things ready... but when I put the setTimeout in an infinte loop, it prevents the page from loading.

Any clues how to do the task????

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Use setInterval function instead it will trigger each period of time.

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

0

The setInterval function is the best way to do the task... but an alternative is recursion :

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda