Search code examples
jqueryloopselementrepeatskip

Jquery: how to skip first element time


in this code snippet, i want to decrease (skip) the time of the first element because it takes a long time to appear on the screen (10000), is it possible ?!

$(document).ready(
    function()
    {
        var i = 0;
        setInterval(
        function() {
             $('.handler').html(i + "<br />");
             i = (++i) % 3;
        }
        , 10000);
    }
);

the result of this code:

screen empty
1
2
3
1
2
3
and so on ...

Thanks to @aTo


Solution

  • The simplest thing to do would be to declare the timer function first, and call it just once, before starting the setInterval:

    var i = 0;
    function timer() {
         $('.handler').html(i + "<br />");
         i = (i + 1) % 3;
    }
    
    timer();
    setInterval(timer, 10000);