数值限时增长显示效果

    window.onload = function(){
        var number =document.getElementById("number");
        var oTimer = 0;
        var n = 0;

        var oTimer = setInterval(function(){
              //这里的10000是下面数字100000的十分之一,效果还行,可以根据自己需求调节两个数字
              n += Math.floor(Math.random()*10000);

              if(n >= 100000){
                number.innerHTML = formatCurrency(100000);
                clearInterval(oTimer);
              }else{
                number.innerHTML = formatCurrency(n);
              }

              
        },20);
    }

    function formatCurrency(num) {    
        num = num.toString().replace(/\$|\,/g,'');    
        if(isNaN(num))    
        num = "0";    
        sign = (num == (num = Math.abs(num)));    
        num = Math.floor(num*100+0.50000000001);    
        cents = num%100;    
        num = Math.floor(num/100).toString();    
        if(cents<10)    
        cents = "0" + cents;   
        for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)    
        num = num.substring(0,num.length-(4*i+3))+','+    
        num.substring(num.length-(4*i+3));    
        return (((sign)?'':'-') + num);    
    }

 

posted on 2018-06-06 14:06  婆婆买了只kitty  阅读(115)  评论(0编辑  收藏  举报

导航