Answer the question
In order to leave comments, you need to log in
How to count digits from 0 to number in js with fixed rate?
How to count digits from 0 to number in js with fixed rate?
For example, I have the number 20 and 30000.
I need the countdown to end at the same moment
Making a simple counter is not a problem, but I don’t understand how to specify the time
Answer the question
In order to leave comments, you need to log in
Like this?
{
const max1 = 9;
const max2 = 30; // max of two nums
let cur = max2 / max1;
const inc = cur;
const len = 3;
const log = (a, b) => {
console.log(b > -Infinity ? `a: ${` ${a}`.slice(-len)} | b: ${` ${b}`.slice(-len)}` : `a: ${` ${a}`.slice(-len)} |`)
}
log(0, 0);
for(let i = 1, j = 1; i < max2; i++) {
const nums = [i];
if(i >= cur) {
nums.push(j);
j++;
cur += inc;
}
log(...nums);
}
log(max2, max1);
}
Determine once the start time and the end time of the animation - they are common to all counters.
At any time, you can calculate the current value from the proportion (now - start) / (end - start) = x / total
, from where x = total * (now - start) / (end - start)
For example, requestAnimationFrame()
call a function that will draw the next frame through: the current values of the counters.
JS does not have an exact timer, the best you can use is a setTimeout series, but the more timeouts, the more error will accumulate, experiment, see if the result suits you.
Let there be a number N (in your version - 20 or 3000). And there is a time T for which you need to calculate. We display zero. Then we wait for the time T/N and output one. Well, further similarly.
Waiting must be started BEFORE the number is displayed.
There may be a problem when the computer does not have time to display numbers in a row. Then we look at how much time has passed, and display not the next number, but the one for which the time has come.
Depending on the distance, adjust delay so that the error is not large,
I hope I helped you) change the value num1=30000 num2=20 you can set delay 0.6
to find out the required delay range, you need to write this formula num2 / num 1 * 1000 = 0.6 you can select everything values up to 0.6
____SCHOOLCHILDREN:
I don't need math and physics :))____ result ->
I'm wondering why you need this algorithm?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question