Thank you for being a valued part of the CNET community. As of December 1, 2020, the forums are in read-only format. In early 2021, CNET Forums will no longer be available. We are grateful for the participation and advice you have provided to one another over the years.

Thanks,

CNET Support

Question

I Need help with JavaScript?

Jul 10, 2016 3:26AM PDT

I need help JavaScript functions . So I 've a file JS and It has some functions and I need add year to this function . Please look at the a code.
{code}
/* Start Countdown Settings */

var startDate = new Date("10/07/2016"); //THIS IS JUST FOR REFERENCE- DO NOT CHANGE THIS.

var endDate = new Date("01/01/2017"); //CHANGE THIS TO YOUR LAUNCHING DATE

var dif = endDate.getTime() - startDate.getTime();
var difToSecond = dif / 1000;
var defaultPercent = 0;


$(function() {
$('#counter').countdown({
until: endDate,
layout: '<div></div>',
onTick: updateBar
});

$('a[rel=tooltip]').tooltip();
$('div[rel=tooltip]').tooltip();
});

function updateBar(periods) {

fillSecondBar(periods[6]);
fillMinuteBar(periods[5]);
fillHourBar(periods[4]);
fillDayBar(periods[3]);

fillTotalbar(periods[6] + periods[5] * 60 + periods[4] * 60 * 60 + periods[3] * 60 * 60 * 24);
}

function fillSecondBar(percent) {
$('#second-number').html(percent);
$('#second-bar').css('width', percent * 100 / 60 + '%');
}

function fillMinuteBar(percent) {
$('#minute-number').html(percent);
$('#minute-bar').css('width', percent * 100 / 60 + '%');
}

function fillHourBar(percent) {
$('#hour-number').html(percent);
$('#hour-bar').css('width', percent * 100 / 24 + '%');
}

function fillDayBar(percent) {
$('#day-number').html(percent);
$('#day-bar').css('width', percent * 100 / 365 + '%');
}

function fillTotalbar(percent) {
defaultPercent = 100 - 100 * percent / difToSecond;

if (defaultPercent >= 10) {
currentPercent = defaultPercent.toString().substr(0, 5);
} else {
currentPercent = defaultPercent.toString().substr(0, 4);
}

$('#total-bar').css('width', defaultPercent + '%').html(currentPercent + '%');
}
{/ code}
So that is mine code. If you understand please help me! Hoq I can add a yer For example 0 - year 0- day 0-minute 0-second

Discussion is locked

- Collapse -
Answer
I worry about this effort.
Jul 10, 2016 8:09AM PDT

Your post has more than a few typos and misspellings so I worry this is hampering your coding efforts. People might be able to read past these errors but computers will just spew error messages.

I don't see any year functions in your code. As this is your code (you claimed it's yours) why is that missing?

- Collapse -
Answer
Jul 11, 2016 3:13AM PDT

Hi I think you don't understand my question?
I said that I created day, minut, second, hour. So I need to add year! In this code hasn't year! I need that! I said that how I can add year!? I don't add year yet!

- Collapse -
Re: add year
Jul 11, 2016 3:21AM PDT

First define the rules. Then add the necessary code.

Since a year can be 365 days or 366 days, you have to define when 366 days is 1 year, 0 days and when it is 1 year, 1 day. That determines if 400 days is 1 year, 35 days or 1 year, 34 days
If you would want to add months also, that has other complexitities, since a month can be 28, 29, 30 or 31 days. So it makes some sense to not do that initially.

- Collapse -
Thanks For Answer!
Jul 11, 2016 4:22AM PDT

O! Thanks! For your answer! I understand! I've had that problem. Thanks for yours help!