I want to make a trigger an adding score effect when user’s score changing.
Here is the code I currently have:
let speed = 200;
let div = document.querySelector('div')
let olddiv = parseFloat(div.textContent)
let interval;
function change(increase){
let add = increase/speed
interval = setInterval(function(){
div.textContent = parseFloat(div.textContent) +add;
if(div.textContent > olddiv + increase){
div.textContent = olddiv+ increase
window.clearInterval(interval)
}
},1)
}
change(5000)
<div>1000</div>
The code works fine but I add 25 (increase/speed value) per milliseconds.
I want to make the score adding 1 every times. I tried to use for
loop, but for
loop will not shows the adding effect.
Also, since the setInterval
doesn’t accept milliseconds lower than 1, I can’t use this code as well (this code will execute 1 ms every time which is too slow.
let speed = 200;
let div = document.querySelector('div')
let olddiv = parseFloat(div.textContent)
let interval;
function change(increase){
let addspeed = speed/increase;
interval = setInterval(function(){
div.textContent = parseFloat(div.textContent) +1;
if(div.textContent > olddiv + increase){
div.textContent = olddiv+ increase
window.clearInterval(interval)
}
},addspeed)
}
change(5000)
<div>1000</div>
Could anyone give me any solution of solving this problem?
Thanks for any responds!