Loop timer in JavaScript

前端 未结 6 876

I need to execute a piece of JavaScript code say, each 2000 milliseconds.

setTimeout(\'moveItem()\',2000)

The above will execute a function

相关标签:
6条回答
  • 2020-11-28 21:25

    I believe you are looking for setInterval()

    0 讨论(0)
  • 2020-11-28 21:26
    setInterval(moveItem, 2000);
    

    is the way to execute the function moveItem every 2 seconds. The main problem in your code is that you're calling setInterval inside of, rather than outside of, the callback. If I understand what you're trying to do, you can use this:

    function moveItem() {
        jQuery('.stripTransmitter ul li a').trigger('click');
    }
    
    setInterval(moveItem, 2000);
    

    N.B.:Don't pass strings to setTimeout or setInterval - best practice is to pass an anonymous function or a function identifier (as I did above). Also, be careful to not mix single and double quotes. Pick one and stick with it.

    0 讨论(0)
  • 2020-11-28 21:30

    It should be:

    function moveItem() {
      jQuery(".stripTransmitter ul li a").trigger('click');
    }
    setInterval(moveItem,2000);
    

    setInterval(f, t) calls the the argument function, f, once every t milliseconds.

    0 讨论(0)
  • 2020-11-28 21:31

    You should try something like this:

     function update(){
        i++;
        document.getElementById('tekst').innerHTML = i;
        setInterval(update(),1000);
        }
    

    This means that you have to create a function in which you do the stuff you need to do, and make sure it will call itself with an interval you like. In your body onload call the function for the first time like this:

    <body onload="update()">
    
    0 讨论(0)
  • 2020-11-28 21:46

    Here the Automatic loop function with html code. I hope this may be useful for someone.

    <!DOCTYPE html>
    <html>
    <head>
    <style>
    div {
    position: relative;
    background-color: #abc;
    width: 40px;
    height: 40px;
    float: left;
    margin: 5px;
    }
    </style>
    <script src="http://code.jquery.com/jquery-latest.js"></script>
    </head>
    <body>
    <p><button id="go">Run »</button></p>
    <div class="block"></div>
    <script>
    
    function test() {
    $(".block").animate({left: "+=50", opacity: 1}, 500 );
       setTimeout(mycode, 2000);
    };
    $( "#go" ).click(function(){
    test();
    });
    </script>
    </body>
    </html>
    

    Fiddle: DEMO

    0 讨论(0)
  • 2020-11-28 21:47

    Note that setTimeout and setInterval are very different functions:

    • setTimeout will execute the code once, after the timeout.
    • setInterval will execute the code forever, in intervals of the provided timeout.

    Both functions return a timer ID which you can use to abort the timeout. All you have to do is store that value in a variable and use it as argument to clearTimeout(tid) or clearInterval(tid) respectively.

    So, depending on what you want to do, you have two valid choices:

    // set timeout
    var tid = setTimeout(mycode, 2000);
    function mycode() {
      // do some stuff...
      tid = setTimeout(mycode, 2000); // repeat myself
    }
    function abortTimer() { // to be called when you want to stop the timer
      clearTimeout(tid);
    }
    

    or

    // set interval
    var tid = setInterval(mycode, 2000);
    function mycode() {
      // do some stuff...
      // no need to recall the function (it's an interval, it'll loop forever)
    }
    function abortTimer() { // to be called when you want to stop the timer
      clearInterval(tid);
    }
    

    Both are very common ways of achieving the same.

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