Count animation from number A to B

后端 未结 8 980
一个人的身影
一个人的身影 2020-11-30 22:18

I am updating a numeric value inside an element by doing intervalled ajax requests.

To make the whole thing a bit more alive,

8条回答
  •  春和景丽
    2020-11-30 22:44

    Here is one version where increments grow by some defined multiplier (mul). frameDelay is the time delay for each increment. This looks a bit better if you have values that camn

    function cAnimate(id, start, end, frameDelay = 100, mul = 1.2) {
        var obj = document.getElementById(id);
        var increment = 2;
        var current = start;
        var timer = setInterval(function() {
            current += increment;
            increment *= mul;
            if (current >= end) {
                current = end;
                clearInterval(timer);
            }
    
            obj.innerHTML = Math.floor(current).toLocaleString();
    
        }, frameDelay);
    }
    
    cAnimate("counter", 1, 260000, 50);
    

提交回复
热议问题