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

193
Views
Show variable value each time loop runs not just final outcome

I'm trying to display a countdown from 10 in my HTML.

This comes from a for loop which decrements down from 10.

The problem I have is since the loop counts down and stops at 0 the value I'm displaying is the final value rather than printing all the values (if that makes sense)!

How can I display all of them in my HTML?

Code:

    let countdown = 'Countdown 10';

for (let i = 10; i >= 0; i--) {
    if (i === 10) {
    countdown = 'Countdown 10';
    } else if (i === 0) {
    countdown = 'Blast off!';
    } else { 
    countdown = i;
    }
        console.log(countdown);
    };
    

    let output = document.querySelector('.output');
output.innerHTML = '';

const para = document.createElement('p');
para.textContent = countdown;
output.appendChild(para);

I'm sure I can do something with the para.textContent e.g. putting it into the loop. But I'm not sure how.

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

0

you should assign the value to the HTML element and append that element to document inside the loop block with a delay

let delay = 300;
let count = 10;
const output = document.querySelector('.output');
const para = document.createElement('p');
output.appendChild(para);

for (let i = count; i >= 0; i--) {
    let countdown = i;
    if (i === 10) {
        countdown = 'Countdown 10';
    } else if (i === 0) {
        countdown = 'Blast off!';
    }
    setTimeout(()=>{
      para.textContent = countdown;
    }, (count - i) * delay)
};
<div class="output"></div>

about 4 years ago · Juan Pablo Isaza Report

0

You just need to set up your elements first, so you can write to them along the way. Using a for loop will cycle through so fast, you won't see the countdown happen, so I also show an alternate version using setInterval, which allows you to control the time in between each count.

let countdown = 'Countdown 10';

let output = document.querySelector('.output');
output.innerHTML = '';

const para = document.createElement('p');
output.appendChild(para);


const para2 = document.createElement('p');
output.appendChild(para2);

for (let i = 10; i >= 0; i--) {
  if (i === 10) {
    countdown = 'Countdown 10';
  } else if (i === 0) {
    countdown = 'Blast off!';
  } else {
    countdown = i;
  }
  para.textContent = countdown;
  //console.log(countdown);
};

para2.textContent = 'Countdown 10'
let i = 9,
  intv = setInterval(() => {
    if (i < 0) return clearInterval(intv);
    let countdown = i === 0 ? 'Blast off!' : i
    para2.textContent = countdown;
    i--
  }, 1000)
<div class='output'>
</div>

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!