Countdown in Javascript?

Put the dates in an array. Be careful, in Javascript the months are zero-based so ranging from 0 to 11. Iterate the array and when the date is bigger then today display the days in between: var calcPayDate = function () { var payDates = ; payDates.

Push(new Date(2011, 0, 1)); payDates. Push(new Date(2011, 1, 15)); payDates. Push(new Date(2011, 2, 12)); payDates.

Push(new Date(2011, 3, 20)); var today = new Date(); for (var I = 0; I today) { document. GetElementById('countdownDiv'). InnerHTML = calcDays(payDatesi, today); break; } } } var calcDays = function(date1, date2) { // The number of milliseconds in one day var ONE_DAY = 1000 * 60 * 60 * 24 // Convert both dates to milliseconds var date1_ms = date1.getTime() var date2_ms = date2.getTime() // Calculate the difference in milliseconds var difference_ms = Math.

Abs(date1_ms - date2_ms) // Convert back to days and return return Math. Round(difference_ms / ONE_DAY) } The calcDays function is an function found on this site The days are put in a div which is called 'countdownDiv.

Put the dates in an array. Be careful, in Javascript the months are zero-based so ranging from 0 to 11. Iterate the array and when the date is bigger then today display the days in between: var calcPayDate = function () { var payDates = ; payDates.

Push(new Date(2011, 0, 1)); payDates. Push(new Date(2011, 1, 15)); payDates. Push(new Date(2011, 2, 12)); payDates.

Push(new Date(2011, 3, 20)); var today = new Date(); for (var I = 0; I today) { document. GetElementById('countdownDiv'). InnerHTML = calcDays(payDatesi, today); break; } } } var calcDays = function(date1, date2) { // The number of milliseconds in one day var ONE_DAY = 1000 * 60 * 60 * 24 // Convert both dates to milliseconds var date1_ms = date1.getTime() var date2_ms = date2.getTime() // Calculate the difference in milliseconds var difference_ms = Math.

Abs(date1_ms - date2_ms) // Convert back to days and return return Math. Round(difference_ms / ONE_DAY) } The calcDays function is an function found on this site The days are put in a div which is called 'countdownDiv'.

Am I doing this wrong: jsfiddle. Net/fWYrM? – dannymcc Feb 28 at 12:04 @dannymcc: You just have to call the actual function: jsfiddle.Net/fWYrM/1 – pimvdb Feb 28 at 12:06 Like @pimvdb said.

Call the function in the onload of the body. Or use a framework like jQuery to bind it to the onload event. – TurBas Feb 28 at 12:32.

Search the web for "JavaScript tutorial". Meanwhile, here's some code to get you started: var dates = new Date(2011, 0, 1), // note that format is year, month-1, day new Date(2011, 1, 15), // don't ask me why new Date(2011, 2, 12), new Date(2011, 3, 20) ; var now = new Date(); for (var I in dates) { // this is a foreach loop if (now Ceil((datesi - now) / 86400000)); break; } }.

1 In your code, variable I is not initialized and you use for-in loop to iterate through array. Both are considered harmful. You should edit your code.

– duri Feb 28 at 11:51 @duri "i is not initialised" - but it's given a value first time it's used! "you use for-in loop to iterate through array" - what's wrong with that? – Stewart Feb 28 at 19:12 It doesn't matter whether it's given a value or not.

Using var prevents the variable from leaking to the global scope. + see stackoverflow. Com/questions/500504/… – duri Feb 28 at 20:10 @duri You didn't say anything about using var.

You talked about initialising the variable - which in my vocabulary, means giving it a starting value. But I agree that using var is a good idea - I had just forgotten it on this occasion. And I'll have to investigate the for..in debate more closely.... – Stewart Mar 1 at 0:56 To initialise a variable means to use var keyword.

Sorry for the confusion. – duri Mar 1 at 7:52.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions


Thank You!
send