How can i make a number add +1 every millisecond with javascript | Sololearn: Learn to code for FREE!

+1

How can i make a number add +1 every millisecond with javascript

eg..- 0+1,+1+1+1+1+1

11/10/2020 5:14:10 AM

Real Kiddie

1 Answer

New Answer

+5

Actually processing the addition of 1 to a number every millisecond consistently and accurately is most likely not going to happen as there will be loss due to the time it takes to process the calculations. You may be better using Date.getTime() to set a variable to the current time then use a loop (or not, depending) to set another variable to the difference between the next call to getTime() and the original call. This will effectively give you the count in milliseconds of time passed between calls with much less loss in accuracy due to processing, since each new call will account for that loss. You could also use a starting number held as an offset and then add/subtract the offset from the result to get your end number. https://www.w3schools.com/jsref/jsref_gettime.asp