How to use requestAnimationFrame to achieve the effect of price reduction?

< span class= "price" data-price= "123.59" data-starttime= "1509809876" > 123.59
< span class= "price" data-price= "255.76" data-starttime= "1509810123" > 255.76

there are several similar forms in the list page, with a price reduction of 0.10 yuan per second, then the number in the span continues to become smaller. The current problem is that each time the first price will move, and the rest will not move. Another problem is that I use settimeout to constantly get the current time and data-starttime to determine whether the price reduction time has begun, resulting in faster and faster animation flatness of requestAnimationFrame

.

ask for advice

Mar.23,2021

I don't understand. For this kind of DOM operation, the update frequency is not high. Generally, requestAnimationFrame is not needed. Is it not possible to go directly through setInterval (func, 1000) ?

for only the first item price will move, it may be the wrong DOM query.

if you use jq, the approximate code is as follows:

setInterval(function() {
    var time = new Date().getTime()
    $('.price').each(function() {
        if(time > $(this).attr('data-starttime')) {
             $(this).text(parseInt($(this).text()) - 0.1);
        }
    })
}, 1000)
Menu