user3171894
user3171894

Reputation:

i am facing issue with for loop in jQuery

link

i am facing problem with for loop in jQuery

i want to show value one by one

1 then 2 then 3 upto 50

but it showing 1 to 50 at the same time

html

<div><span></span></div>

js

var i=0;


for(i=0;i<=50;i++)
{
    $("div").hide().append("<span>" + i + "</span>" + "\n").fadeIn(500);

}

Upvotes: 1

Views: 60

Answers (2)

Anoop Joshi P
Anoop Joshi P

Reputation: 25527

You can do like this

for (i = 0; i <= 50; i++) {
    $("div").append($("<span/>", {
        text: i
    }).hide().delay(i * 400).fadeIn(300));

}

Fiddle

Upvotes: 0

Bhushan Kawadkar
Bhushan Kawadkar

Reputation: 28513

Try this : instead of loop, you can use setInterval where append the count value and increment the count.

var i = 0;
var limit = 50;

var interval = setInterval(function () {
    $("div").append("<span>" + i + "</span>" + "\n").fadeIn(500);

    if (i == limit) clearInterval(interval); //stop interval
    i++;
}, 2000);

Demo

Upvotes: 2

Related Questions