Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

JavaScript – Initial value to Target value within a set time

I have an integer value that I want to take from the initial value of 0 to 100 over the span of 10 seconds; However, I don’t want it to start from 0 and go instantly to 100 within that time-span, I want it to climb up to that value overtime, so from 0 to 1 to 2, etc. How could this effect be achieved?

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You can use setInterval and update the value:

    let value = 0;
    
    let interval = setInterval(function () {
      if(value >= 10) {
        clearInterval(interval);
        return 
      }        

      value++;
      
      document.getElementById("value").innerHTML = value;
    }, 1000);
<div id="value">0</div>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading