jquery “animate” variable value

后端 未结 9 783
说谎
说谎 2021-02-04 05:36

I need to \"animate\" a variable with jquery.

Example: The variable value is 1. The value should be 10 after 5 seconds. It should be increase \"smoothl

相关标签:
9条回答
  • 2021-02-04 06:14

    Html mark up as
    Html

    <span id="changeNumber">1</span>
    

    You can change its value after 5 seconds.
    JQuery:

    setInterval(function() {        
            $('#changeNumber').text('10');
        },5000);
    

    Here is a Demo http://jsfiddle.net/Simplybj/Fbhs9/

    0 讨论(0)
  • 2021-02-04 06:14
    ​var blub = 1;
    setTimeout(function () {
        blub = 10;
    }, 5000);
    
    0 讨论(0)
  • 2021-02-04 06:20

    Use setInterval :

    percentage = 0;
    startValue = 1;
    finishValue = 5;
    currentValue = 1;
    interval = setInterval(function(){ 
       percentage ++; 
       currentValue = startValue + ((finishValue - startValue) * percentage) / 100;
       doSomething(currentValue);
       if (percentage == 100) clearInterval(interval);
     }, duration / 100)
    
    function doSomething(val) { /*process value*/}
    
    0 讨论(0)
提交回复
热议问题