CairoCoder
CairoCoder

Reputation: 3207

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

Upvotes: 1

Views: 266

Answers (2)

Alnitak
Alnitak

Reputation: 340045

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);

Upvotes: 2

adeneo
adeneo

Reputation: 318342

Put your code in a function, run the function on pageload and then use an interval to run the function every ten seconds:

$(function() {
    var i = 0;

    go();
    setInterval(go, 10000);

    function go() {
       $('.handler').html(i + "<br />");
       i = (++i) % 3;
    }
});

Upvotes: 2

Related Questions