How do I correctly use setInterval and clearInterval to switch between two different functions?

前端 未结 6 431
-上瘾入骨i
-上瘾入骨i 2020-12-01 12:53

For practice I am trying to display a number that increments from 0 - 9, then decrements from 9 - 0, and infinitely repeats.

The code that I have so far seems to

相关标签:
6条回答
  • 2020-12-01 13:14

    @Claude, you are right, the other solution I proposed was too different from original code. This is another possible solution, using setInterval and switching functions:

    function onloadFunctions() {
        var count = 0;
        var refId = null;
        var target = document.getElementById("aux");
    
        var countUp = function() {
            target.innerHTML = count;
            count ++;
            if(count >= 9) {
                window.clearInterval(refId);
                refId = window.setInterval(countDown, 500);
            }
        }
    
        var countDown = function() {
            target.innerHTML = count;
            count --;
            if(count <= 0) {
                window.clearInterval(refId);
                refId = window.setInterval(countUp, 500);
            }
        }
        refId = window.setInterval(countUp, 500);
    }
    
    0 讨论(0)
  • 2020-12-01 13:17

    try this:

    ...
    <body onload = "onloadFunctions();">
    
        <script>
            var cup, cdown; // intervals
            var count = 0,
                here  = document.getElementById("here");
    
            function onloadFunctions() {
                cup = setInterval(countUp, 200);
            }
    
            function countUp() {
                here.innerHTML = count;
                count++;
    
                if(count === 10) {
                    clearInterval(cup);
                    cdown = setInterval(countDown, 200);
                }
            }
            function countDown() {   
                here.innerHTML = count;
                count--;
    
                if(count === 0) {
                    clearInterval(cdown);
                    cup = setInterval(countUp, 200);
                }       
            }
        </script>
    
        From 0 - 9, up and down:&nbsp;&nbsp;<div id = "here"></div>
    </body>
    

    you could also create a single reference to #here element. Use always === instead of ==

    0 讨论(0)
  • 2020-12-01 13:23

    clearInterval(this);. You can't do that. You need to save the return value from setInterval.

    var interval;
    function onloadFunctions()
    {
        countUp();
        interval = setInterval(countUp, 200);
    }
    
    var count = 0;
    function countUp()
    {
        document.getElementById("here").innerHTML = count;
        count++;
    
        if(count == 10)
        {
            clearInterval(interval);
            countDown();
            interval = setInterval(countDown, 200);
        }
    }
    function countDown()
    {
        document.getElementById("here").innerHTML = count;
        count--;
    
        if(count == 0)
        {
            clearInterval(interval);
            countUp();
            interval = setInterval(countUp, 200);
        }       
    }
    
    0 讨论(0)
  • 2020-12-01 13:24
        /** Tools */
    const log = require('ololog').configure({
      locate: false
    })
    
    let count = 0
    let interval__UP
    let interval__DOWN
    
    function countUp () {
      count++
      log.green('countUp(): ', count)
    
      if (count == 5) {
        clearInterval(interval__UP)
        interval__DOWN = setInterval(function () {
          countDown()
        }, 1000)
      }
    
    }
    
    function countDown () {
    
      count--
      log.red('countDown(): ', count)
    
      if (count == 0) {
        clearInterval(interval__DOWN)
        interval__UP = setInterval(function () {
          countUp()
        }, 3000)
      }
    }
    
    function start () {
      countUp()
      log.cyan('start()')
      interval__UP = setInterval(function () {
        countUp()
      }, 2000)
    }
    
    start()
    

    Console Log shows it's working

    0 讨论(0)
  • 2020-12-01 13:30

    You need to capture the return value from setInterval( ... ) into a variable as that is the reference to the timer:

    var interval;
    var count = 0;
    
    function onloadFunctions()
    {
        countUp();
        interval = setInterval(countUp, 200);
    }
    
    /* ... code ... */
    
    function countUp()
    {
        document.getElementById("here").innerHTML = count;
        count++;
    
        if(count === 10)
        {
            clearInterval(interval);
            countUp();
            interval = setInterval(countUp, 200);
        }
    }
    
    0 讨论(0)
  • 2020-12-01 13:31

    There are many ways to solve this problem, the following is my suggestion:

    function onloadFunctions() {
        var count = 0;
        var delta = 1;
        var target = document.getElementById("here");
        var step = function() {
            if(count <= 0) delta =  1;
            if(count >= 9) delta = -1;
            count += delta;
            target.innerHTML = count;
            window.setTimeout(step, 500);
        }
        step ();
    }
    

    PS: it's safer to use setTimeout than setInteval.

    0 讨论(0)
提交回复
热议问题