user1260827
user1260827

Reputation: 1520

jquery countdown plugin: no countdown, only zeros

I want to create 200 divs and timer for each div. This is my code:

$(document).ready(function() {

    for(var i=0; i<200; i++)
    {
        var tag = '<div id="' + i +'" style="width:150px"></div><br/>';
        $('#rsr').append(tag);
    }

    for(var i=0;i<200; i++)
    {
    var date = randomDate(new Date(2012, 0, 1), new Date())

        $("#"+i).countdown({until: date});
    }
    });

    function randomDate(start, end) {
        return new Date(start.getTime() + Math.random() * (end.getTime() - start.getTime()))
    }

Where is a error?
Thanks. JsFiddle

Upvotes: 0

Views: 192

Answers (3)

JJJ
JJJ

Reputation: 33163

The problem is simply that you are creating a date between January 1st 2012 and today, so it's always in the past. If the target date has already passed, the plugin stops at zero.

Pick for example 2013 instead:

var date = randomDate(new Date(2013, 0, 1), new Date())

You should fix the id problem (they shouldn't start with a number), but most browsers can cope with it easily so it's not the actual cause.

Fiddle: http://jsfiddle.net/SDFLn/17/

Upvotes: 1

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167172

Please replace this part of your code:

for(var i=0;i<200; i++)
{
    var date = randomDate(new Date(2012, 0, 1), new Date());
    $("#s"+i).countdown({until: date});
}

You cannot have id with numbers as starting character. And don't forget to end the lines with a semicolon ;.

Upvotes: 0

mgraph
mgraph

Reputation: 15338

id can not start with number try this:

for(var i=0; i<200; i++)
{
    var tag = '<div id="cnt_' + i +'" style="width:150px"></div><br/>';
    $('#rsr').append(tag);
}

for(var i=0;i<200; i++)
{
var date = randomDate(new Date(2012, 0, 1), new Date())

    $("#cnt_"+i).countdown({until: date});
}
})

instead of id="'+i+'" do id="cnt_'+i+'"

Upvotes: 0

Related Questions