Increment integer by 1; every 1 second

前端 未结 3 1984
梦如初夏
梦如初夏 2021-01-03 03:37

My aim is to create identify a piece of code that increments a number by 1, every 1 second:

We shall call our base number indexVariable, I then want to: indexVaria

相关标签:
3条回答
  • 2021-01-03 03:51

    You can use setInterval() for that reason.

    var i = 1;
    
    var interval = setInterval( increment, 1000);
    
    function increment(){
        i = i % 360 + 1;
    }
    

    edit: the code for your your followup-question:

    var interval = setInterval( rotate, 1000);
    
    function rotate(){
          percentArrow.rotate(1,150,150);
    }
    

    I'm not entirely sure, how your rotate works, but you may have to store the degrees in a var and increment those var too like in the example above.

    0 讨论(0)
  • 2021-01-03 03:51

    Try:

    var indexVariable = 0;
    setInterval(
        function () {
            indexVariable = (indexVariable + 1) % 361;
        }, 1000}
    
    0 讨论(0)
  • 2021-01-03 04:07
    var indexVariable = 0;
    setInterval(function () {
        indexVariable = ++indexVariable % 360 + 1; // SET { 1-360 }
    }, 1000);
    
    0 讨论(0)
提交回复
热议问题