+ 1

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

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

10th Nov 2020, 5:14 AM
JABIR AHMAD MUHAMMAD
JABIR AHMAD MUHAMMAD - avatar
2 Answers
+ 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
10th Nov 2020, 6:10 AM
ChaoticDawg
ChaoticDawg - avatar
0
Yes you can use setInterval() Method syntax: setInterval(functionName , 1000); //in miliseconds ex -> let a = 0; setInterval(function(){ console.log(`The value is : ${a}`); a += 10; }, 1 * 1000); //set interval will run continuous infinitely
1st Jun 2022, 11:43 AM
Praween Gupta
Praween Gupta - avatar