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

148
Visualizações
Set an element attribute multiple times within a javascript function

im using Vanilla Javascript, and my problem is that i need to render a progress bar in a certain time, but JS only render the final step...

'use strict'
var button = document.getElementById('button');

function sleep(milliseconds) {
    const date = Date.now();
    let currentDate = null;
    do {
      currentDate = Date.now();
    } while (currentDate - date < milliseconds);
  }

function initProgressBar()
{
    let progressBar = document.querySelector('#progress-bar'),
    count = 25,
    total = 64;
    progressBar.style = `width:${count}%;`;

    while (total > 0) {
        sleep(300);
        total = Math.trunc(total/2);
        count = count + total;
        progressBar.style = `width:${count}%;`;
        
    }
}

button.onclick = initProgressBar;
<!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>
    <style>
        #progress-container {
            height: 4px;
            width: 100%;
        }

        #progress-bar {
            width: 0%;
            height: 100%;
            background-color: blueviolet;
        }


    </style>
</head>
<body>
    <div id="progress-container">
        <div id="progress-bar"></div>
    </div>
    <button id=button>press</button>
    <script src='prueba.js'></script>
</body>
</html>

The idea is that when the user clicks the button, the progress bar automatically jumps to 25% and then progressively goes to ~ 88% (after that I'll send it to 100% using another function)

The problem is that Javascript only update the element on his last value (88%).

So, is there any way to set the attribute of an element multiple times inside a javasciript function?

about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

The way sleep will block the execution thread and might lead to unexpected behaviour in some cases. A better implementation of sleep could be done using Promise and setTimeout.

Here's the snippet using the modified sleep, that works as expected :

'use strict'
var button = document.getElementById('button');

function sleep(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}

async function initProgressBar()
{
    let progressBar = document.querySelector('#progress-bar'),
    count = 25,
    total = 64;
    progressBar.style = `width:${count}%;`;

    while (total > 0) {
        await sleep(300);
        total = Math.trunc(total/2);
        count = count + total;
        progressBar.style = `width:${count}%;`;
    }
}

button.onclick = initProgressBar;
<!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>
    <style>
        #progress-container {
            height: 4px;
            width: 100%;
        }

        #progress-bar {
            width: 0%;
            height: 100%;
            background-color: blueviolet;
        }


    </style>
</head>
<body>
    <div id="progress-container">
        <div id="progress-bar"></div>
    </div>
    <button id=button>press</button>
    <script src='prueba.js'></script>
</body>
</html>

about 4 years ago · Santiago Trujillo Relatório

0

The source of the problem is likely the total = Math.trunc(total/2); construction. You're dividing by 2 and then rounding down.

So it's goal is preset to 25 + 64 = 89, meaning 89% is the end.

Once it gets to 88%, it takes that remaining 1, divides it by 2 (1 / 2 = 0.5) and then it rounds that down, so it becomes 0, 88 + 0 = 88, which still isn't 89, so it'll just end up in an infinite loop there.

about 4 years ago · Santiago Trujillo 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