Setting a custom countdown in javascript - javascript

My code is located below. I'm trying to set a countdown, however I can't figure out how to set the timer to operate. I've tried changing the "var setting". When I do it seems that the countdown works, however it glitches and the numbers appear but all go back to "0". Really confused. Someone Please help!
(function($) {
$.fn.countdown = function(options) {
var settings = { 'date': "30 september 2014 16:24:00" };
if(options) {
$.extend(settings, options);
}
this_sel = $(this);
function count_exec() {
eventDate = Date.parse(settings['date']) / 1000; // Parse the date string
currentDate = Math.floor($.now() / 1000); // Find the timestamp for now
seconds = eventDate - currentDate; // Find the number of seconds remaining
if (seconds <= 0) { // After the event date has passed
days = 0;
hours = 0;
minutes = 0;
seconds = 0;
} else {
days = Math.floor(seconds / (60 * 60 * 24)); // Divide to find the number of days remaining
seconds -= days * 60 * 60 * 24; // Subtract the number of (complete, => 24 hours) days calculated above
hours = Math.floor(seconds / (60 * 60)); // Get the number of hours from that modified number ^
seconds -= hours * 60 * 60;
minutes = Math.floor(seconds / 60);
seconds -= minutes * 60;
}
this_sel.find('#days').val(days).trigger('change');
this_sel.find('#hours').val(hours).trigger('change');
this_sel.find('#mins').val(minutes).trigger('change');
this_sel.find('#secs').val(seconds).trigger('change');
} // End of count_exec();
count_exec();
interval = setInterval(count_exec, 1000);
} // End of the main function
}) (jQuery);

Please but below code in your file and apply the desire date. It works fine for me. Let me know if any issue.
<body>
<form name="count">
<input type="text" size="69" name="count2">
</form>
<script type="text/javascript">
//change the text below to reflect your own,
var before = "Christmas!"
var current = "Today is Christmas. Merry Christmas!"
var montharray = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
function countdown(yr, m, d) {
theyear = yr; themonth = m; theday = d
var today = new Date()
var todayy = today.getYear()
if (todayy < 1000)
todayy += 1900
var todaym = today.getMonth()
var todayd = today.getDate()
var todayh = today.getHours()
var todaymin = today.getMinutes()
var todaysec = today.getSeconds()
var todaystring = montharray[todaym] + " " + todayd + ", " + todayy + " " + todayh + ":" + todaymin + ":" + todaysec
futurestring = montharray[m - 1] + " " + d + ", " + yr
dd = Date.parse(futurestring) - Date.parse(todaystring)
dday = Math.floor(dd / (60 * 60 * 1000 * 24) * 1)
dhour = Math.floor((dd % (60 * 60 * 1000 * 24)) / (60 * 60 * 1000) * 1)
dmin = Math.floor(((dd % (60 * 60 * 1000 * 24)) % (60 * 60 * 1000)) / (60 * 1000) * 1)
dsec = Math.floor((((dd % (60 * 60 * 1000 * 24)) % (60 * 60 * 1000)) % (60 * 1000)) / 1000 * 1)
if (dday == 0 && dhour == 0 && dmin == 0 && dsec == 1) {
document.forms.count.count2.value = current
return
}
else
document.forms.count.count2.value = "Only " + dday + " days, " + dhour + " hours, " + dmin + " minutes, and " + dsec + " seconds left until " + before
setTimeout("countdown(theyear,themonth,theday)", 1000)
}
//enter the Future count down date using the format year/month/day
countdown(2016, 9, 24)
</script>
</body>

Related

Check if the time distance is between different times

I made a countdown using javascript and php from my functions the countdown works but now I want to have 3 options:
if the countdown is longer than 24 hours show te selector.next(".countdown").html(expiry); date
if the countdown is 6 hours or less show the timer selector.next(".countdown").html(days + "d " + hours + "h " + minutes + "m " + seconds + "s ");
else the countdown is less than 0 show that the endend selector.next(".countdown").html(<p>Closed</p>);
$(".expiry").each(function() {
var expiry = new Date($(this).text());
var selector = $(this)
var x = setInterval(function() {
var currentDateObj = new Date();
var numberOfMlSeconds = currentDateObj.getTime();
var addMlSeconds = 60 * 60 * 1000;
var now = new Date(numberOfMlSeconds - addMlSeconds);
var distance = expiry - now;
var days = Math.floor(distance / (1000 * 60 * 60 * 24));
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
if( distance >= 86400000 && distance < 21600000){
selector.next(".countdown").html(expiry);
}else if ( distance <= 21600000 && distance > 0){
selector.next(".countdown").html(days + "d " + hours + "h " + minutes + "m " + seconds + "s ");
}else{
selector.next(".countdown").html('<p>error</p>');
}
}, 1000);
});
Is the first line of your IF statement correct? Are you wanting to check if it's between those 2 numbers? If so, should the condition not be:
if(distance <= 86400000 && distance > 21600000)
I.E. If the distance is LESS THAN OR EQUAL TO 86400000 AND GREATER THAN 21600000
At the moment you're checking if distance is both GREATER THAN OR EQUAL TO 86400000 and LESS THAN 21600000 which will always produce false
EDIT
See full if statement block with condition for GREATER THAN 86400000
if (distance > 86400000) {
selector.next(".countdown").html('<p>A LOOOOOONG Way off expiring</p>');
}
else if (distance <= 864000000 && distance > 432000000) {
selector.next(".countdown").html(expiry);
}
else if (distance <= 432000000 && distance > 0) {
selector.next(".countdown").html(days + "d " + hours + "h " + minutes + "m " + seconds + "s ");
}
else if (distance < 0) {
selector.next(".countdown").html('<p>afgelopen</p>');
}
else {
selector.next(".countdown").html('<p>Error</p>');
}

How to reduce the cyclomatic complexity of this function?

I have a function that takes a timestamp in epoch (like 1517073001) and returns the time that has passed since then in a simple format like "2 hours ago" (not further verbosity like "2 hours, 31 minutes and 15 seconds ago").
The function works as intended but JSHint complains about using too many statements (30) and its cyclomatic complexity being too high (12). I was wondering what could be a way to improve these two aspects.
This is the function:
function msToTime(epoch) {
var previous = new Date(epoch * 1000);
var current = Math.floor(new Date().getTime());
var ms = current - previous;
var years = parseInt((ms / (1000 * 60 * 60 * 24 * 30 * 12)).toFixed(20), 10);
var months = parseInt((ms / (1000 * 60 * 60 * 24 * 30) % 12).toFixed(20), 10);
var days = parseInt((ms / (1000 * 60 * 60 * 24) % 30).toFixed(20), 10);
var hours = parseInt((ms / (1000 * 60 * 60) % 24).toFixed(20), 10);
var minutes = parseInt(ms / (1000 * 60) % 60, 10);
var seconds = parseInt(ms / 1000 % 60, 10);
var formatted = '';
if (years > 0) {
if (years > 1) {
formatted = years + ' years ago';
} else {
formatted = years + ' year ago';
}
} else if (months > 0) {
if (months > 1) {
formatted = months + ' months ago';
} else {
formatted = months + ' month ago';
}
} else if (days > 0) {
if (days > 1) {
formatted = days + ' days ago';
} else {
formatted = days + ' day ago';
}
} else if (hours > 0) {
if (hours > 1) {
formatted = hours + ' hours ago';
} else {
formatted = hours + ' hour ago';
}
} else if (minutes > 0) {
if (minutes > 1) {
formatted = minutes + ' minutes ago';
} else {
formatted = minutes + ' minute ago';
}
} else {
if (seconds > 1) {
formatted = seconds + ' seconds ago';
} else {
formatted = seconds + ' second ago';
}
}
return formatted;
}
var div = document.getElementById('time');
div.innerHTML = msToTime(1517073001);
<div id="time"></div>
Thank you in advance. :)
Another version optimized for divs and modules operations
function msToTime(epoch) {
var value = (Math.floor(new Date().getTime()) - new Date(epoch * 1000)) / 1000;
var time_factors = [['second', 60], ['minute', 60], ['hour', 24], ['day', 30], ['month', 12], ['year', NaN]];
for (factor of time_factors) {
if (value < factor[1] || isNaN(factor[1])) {
var t = Math.floor(value);
return t + ' ' + (t > 1 ? factor[0] + 's' : factor[0]) + ' ago';
}
value /= factor[1];
}
}
Replacing if...else if...else if... by switch (true) and putting the building of singular or plural to a function:
function msToTime(epoch) {
let previous = new Date(epoch * 1000);
let current = Math.floor(new Date().getTime());
let ms = current - previous;
let years = parseInt((ms / (1000 * 60 * 60 * 24 * 30 * 12)).toFixed(20), 10);
let months = parseInt((ms / (1000 * 60 * 60 * 24 * 30) % 12).toFixed(20), 10);
let days = parseInt((ms / (1000 * 60 * 60 * 24) % 30).toFixed(20), 10);
let hours = parseInt((ms / (1000 * 60 * 60) % 24).toFixed(20), 10);
let minutes = parseInt(ms / (1000 * 60) % 60, 10);
let seconds = parseInt(ms / 1000 % 60, 10);
let formatted = '';
function timeAgo(count, word) {
return `${count} ${(count === 1 ? word : word + 's')} ago`
}
switch (true) {
case years > 0:
formatted = timeAgo(years, 'year')
break
case months > 0:
formatted = timeAgo(months, 'month')
break
case days > 0:
formatted = timeAgo(days, 'day')
break
case hours > 0:
formatted = timeAgo(hours, 'hour')
break
case minutes > 0:
formatted = timeAgo(minutes, 'minute')
break
default:
formatted = timeAgo(seconds, 'second')
}
return formatted;
}
time.innerHTML = msToTime(1517073001);
<div id="time"></div>
Defining the date as array and iterating over it has decreased the Cyclomatic complexity number to 4(!), with only 12 statements.
function msToTime(epoch) {
var previous = new Date(epoch * 1000);
var current = Math.floor(new Date().getTime());
var ms = current - previous;
var formatted = '';
var completeDate = [
['year', parseInt((ms / (1000 * 60 * 60 * 24 * 30 * 12)).toFixed(20), 10)],
['month', parseInt((ms / (1000 * 60 * 60 * 24 * 30) % 12).toFixed(20), 10)],
['day', parseInt((ms / (1000 * 60 * 60 * 24) % 30).toFixed(20), 10)],
['hour', parseInt((ms / (1000 * 60 * 60) % 24).toFixed(20), 10)],
['minute', parseInt(ms / (1000 * 60) % 60, 10)],
['second', parseInt(ms / 1000 % 60, 10)]
];
for (var i = 0; i < completeDate.length; i++) {
var amount = completeDate[i][1];
if (amount > 0) {
var unit = completeDate[i][0];
formatted = amount + ' ' + (amount > 1 ? unit + 's' : unit) + ' ago';
break;
}
}
return formatted;
}
var div = document.getElementById('time');
div.innerHTML = msToTime(1517073001);
<div id="time"></div>
Thank you, #connexo, for the important advice!
function msToTime (epoch) {
var previous = new Date(epoch * 1000);
var current = Math.floor(new Date().getTime());
var ms = current - previous;
var years = parseInt((ms / (1000 * 60 * 60 * 24 * 30 * 12)).toFixed(20), 10);
var months = parseInt((ms / (1000 * 60 * 60 * 24 * 30) % 12).toFixed(20), 10);
var days = parseInt((ms / (1000 * 60 * 60 * 24) % 30).toFixed(20), 10);
var hours = parseInt((ms / (1000 * 60 * 60) % 24).toFixed(20), 10);
var minutes = parseInt(ms / (1000 * 60) % 60, 10);
var seconds = parseInt(ms / 1000 % 60, 10);
var formatted = '';
if (years > 0) {
formatted = years > 1 ? years + ' years ago' : years + ' year ago';
} else if (months > 0) {
formatted = months > 1 ? ' months ago' : ' month ago';
} else if (days > 0) {
formatted = days > 1 ? ' days ago' : ' day ago';
} else if (hours > 0) {
formatted = hours > 1 ? ' hours ago' : ' hour ago';
} else if (minutes > 0) {
formatted = minutes > 1 ? ' minutes ago' : ' minute ago';
} else {
formatted = seconds > 1 ? ' seconds ago' : ' second ago';
}
return formatted;
}
var div = document.getElementById('time');
div.innerHTML = msToTime(1417073002);
I have used JS ternary operator to shorten you code. Hope it helps.

How to make countdown function with hourly?

I tried to make countdown function , It is work unless hour time ! But I want to make 2 hours countdown time , I can't figure more ?
<div id="time"></div>
<script type="text/javascript">
var hour = 2 ;
var min = 30;
var countdown = hour * min * 60 * 1000;
//var countdown = hour * 3600 * min * 60 * 1000; also not working
var timeload = setInterval(function () {
countdown -= 1000;
var hr = Math.floor(countdown/(60 * 60 * 1000));
var min = Math.floor(countdown / (60 * 1000));
var sec = Math.floor((countdown - (min * 60 * 1000)) / 1000);
if (countdown <= 0) {
alert("Timeout !");
clearInterval(timeload);
}
else {
$("#time").html("<font color='red'>Allowed Time </font>" + hr + " : " + min + " : " + sec);
}
}, 1000);
</script>
Actual Started Time
Allowed Time 0 : 59 : 59
Expected Start Time Allowed Time 2 : 29 : 59
you can try this solution, you have an error on calculation time (hr, min, sec) it should modulus to maximum each value and also you wrong when convert hour and min to millisecond
var hour = 2 ;
var min = 30;
var countdown = (hour * 60 * 60 * 1000) + (min * 60 * 1000);
var timeload = setInterval(function () {
countdown -= 1000;
var hr = Math.floor( (countdown / (60 * 60 * 1000)) % 24 );
var min = Math.floor( (countdown / (60 * 1000)) % 60 );
var sec = Math.floor( (countdown / 1000) % 60 );
if (countdown <= 0) {
alert("Timeout !");
clearInterval(timeload);
}
else {
$("#time").html("<font color='red'>Allowed Time </font>" + hr + " : " + min + " : " + sec);
}
}, 1000);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="time"></div>
I think your calculations may be wrong. You don't want to multiply the hours by the minutes, but rather add them up in some way.
// useful numbers for calculations into milliseconds
var _1sec = 60 * 1000, _1min = 60 * _1sec, _1hr = 60 * min;
// renamed `hour` to cHR (countdown hour) so I can keep things straight in my head
var total_ms = (cHr * _1hr) + (cMin * _1min);
The hours/minutes/seconds for display also needs reworking.
// Mod (%) takes the remainder after division. So countdown % _1hr takes whatever is left over that doesn't go into whole hours, etc.
hr = Math.floor(countdown / _1hr);
min = Math.floor((countdown % _1hr) / _1min);
sec = Math.floor((countdown % _1min) / _1sec);
an example using diff between start and finish
var hour = 2;
var min = 30;
var finish = new Date();
finish.setHours(finish.getHours() + hour);
finish.setHours(finish.getHours(), finish.getMinutes() + min)
var diff = finish.getTime() - new Date().getTime()
var timeload = setInterval(function() {
diff -= 1000
var s = Math.floor(diff / 1000);
var m = Math.floor(s / 60);
var h = Math.floor(m / 60);
h %= 24;
m %= 60;
s %= 60;
if (diff <= 0) {
alert("FINISH!!");
clearInterval(timeload);
} else {
$("#time").html("<font color='red'>Allowed Time </font>" + h + " : " + m + " : " + s);
}
}, 1000)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="time"></div>

Javascript implementing multiple countdown timers in a table

I have tried to create multiple countdown timers in a table that will rely on variables to set its time left. Currently there are no results output to the page and I'm not sure why.
Ideally, this should display 4 countdown timers
var table;
table = ("<table>");
for (i = 0; i < 4; i++) {
table += ("<tr>");
table += ("<td> Time Left : </td>");
table += ("<td><div id=\"dday" + i + "\"></div></td>");
table += ("<td><div id=\"dhour" + i + "\"></div></td>");
table += ("<td><div id=\"dmin" + i + "\"></div></td>");
table += ("<td><div id=\"dsec" + i + "\"></div></td>");
table += ("</tr>");
}
table += ("</table>");
document.getElementById('listinglist').innerHTML = table;
var yr = 2016,
m = 2,
d = 3,
hr = 4,
min = 5;
for (i = 0; i < 4; i++) {
countdown(yr, m, d, hr, min, i);
}
var year = 2010;
var month = 12;
var day = 21;
var hour = 18;
var minute = 38;
var tz = -5;
var montharray = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
function countdown(yr, m, d, hr, min) {
theyear = yr;
themonth = m;
theday = d;
thehour = hr;
theminute = min;
var today = new Date();
var todayy = today.getYear();
if (todayy < 1000) {
todayy += 1900;
}
var todaym = today.getMonth();
var todayd = today.getDate();
var todayh = today.getHours();
var todaymin = today.getMinutes();
var todaysec = today.getSeconds();
var todaystring1 = montharray[todaym] + " " + todayd + ", " + todayy + " " + todayh + ":" + todaymin + ":" + todaysec;
var todaystring = Date.parse(todaystring1) + (tz * 1000 * 60 * 60);
var futurestring1 = (montharray[m - 1] + " " + d + ", " + yr + " " + hr + ":" + min);
var futurestring = Date.parse(futurestring1) - (today.getTimezoneOffset() * (1000 * 60));
var dd = futurestring - todaystring;
var dday = Math.floor(dd / (60 * 60 * 1000 * 24) * 1);
var dhour = Math.floor((dd % (60 * 60 * 1000 * 24)) / (60 * 60 * 1000) * 1);
var dmin = Math.floor(((dd % (60 * 60 * 1000 * 24)) % (60 * 60 * 1000)) / (60 * 1000) * 1);
var dsec = Math.floor((((dd % (60 * 60 * 1000 * 24)) % (60 * 60 * 1000)) % (60 * 1000)) / 1000 * 1);
document.getElementById('dday').innerHTML = dday;
document.getElementById('dhour').innerHTML = dhour;
document.getElementById('dmin').innerHTML = dmin;
document.getElementById('dsec').innerHTML = dsec;
setTimeout("countdown(theyear,themonth,theday,thehour,theminute)", 1000);
}
<div id="listinglist"></div>
There are few problems in your script, like you are calling the count down function before the montharray is created, you are not using the i part in getElementById etc
var table;
table = ("<table>");
for (i = 0; i < 4; i++) {
table += ("<tr>");
table += ("<td> Time Left : </td>");
table += ("<td><div id=\"dday" + i + "\"></div></td>");
table += ("<td><div id=\"dhour" + i + "\"></div></td>");
table += ("<td><div id=\"dmin" + i + "\"></div></td>");
table += ("<td><div id=\"dsec" + i + "\"></div></td>");
table += ("</tr>");
}
table += ("</table>");
document.getElementById('listinglist').innerHTML = table;
var yr = 2016,
m = 2,
d = 3,
hr = 4,
min = 5;
var year = 2010;
var month = 12;
var day = 21;
var hour = 18;
var minute = 38;
var tz = -5;
var montharray = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
//should be called after setting the montharray
for (i = 0; i < 4; i++) {
countdown(yr, m, d, hr, min, i);
}
function countdown(yr, m, d, hr, min, i) {
theyear = yr;
themonth = m;
theday = d;
thehour = hr;
theminute = min;
var today = new Date();
var todayy = today.getYear();
if (todayy < 1000) {
todayy += 1900;
}
var todaym = today.getMonth();
var todayd = today.getDate();
var todayh = today.getHours();
var todaymin = today.getMinutes();
var todaysec = today.getSeconds();
var todaystring1 = montharray[todaym] + " " + todayd + ", " + todayy + " " + todayh + ":" + todaymin + ":" + todaysec;
var todaystring = Date.parse(todaystring1) + (tz * 1000 * 60 * 60);
var futurestring1 = (montharray[m - 1] + " " + d + ", " + yr + " " + hr + ":" + min);
var futurestring = Date.parse(futurestring1) - (today.getTimezoneOffset() * (1000 * 60));
var dd = futurestring - todaystring;
var dday = Math.floor(dd / (60 * 60 * 1000 * 24) * 1);
var dhour = Math.floor((dd % (60 * 60 * 1000 * 24)) / (60 * 60 * 1000) * 1);
var dmin = Math.floor(((dd % (60 * 60 * 1000 * 24)) % (60 * 60 * 1000)) / (60 * 1000) * 1);
var dsec = Math.floor((((dd % (60 * 60 * 1000 * 24)) % (60 * 60 * 1000)) % (60 * 1000)) / 1000 * 1);
//need to target the elements using i in the id
document.getElementById('dday' + i).innerHTML = dday;
document.getElementById('dhour' + i).innerHTML = dhour;
document.getElementById('dmin' + i).innerHTML = dmin;
document.getElementById('dsec' + i).innerHTML = dsec;
//need to pass the parameter as passed to this function
setTimeout(countdown.bind(this, yr, m, d, hr, min, i), 1000);
}
<div id="listinglist"></div>

Javascript countdown timer for auction site

I am building and auction site and a countdown timer is attached to each product.
i got a working script from here(stackoverflow) -> how to make countdown timer for bidding website
This is my code
<script>
var before = ""
var current = "Ended"
var montharray = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
jQuery.fn.countdown = function(yr, m, d) {
$that = $(this);
theyear = yr;
themonth = m;
theday = d;
var today = new Date();
var todayy = today.getYear();
if (todayy < 1000)
todayy += 1900;
var todaym = today.getMonth();
var todayd = today.getDate();
var todayh = today.getHours();
var todaymin = today.getMinutes();
var todaysec = today.getSeconds();
var todaystring = montharray[todaym] + " " + todayd + ", " + todayy + " " + todayh + ":" + todaymin + ":" + todaysec
futurestring = montharray[m - 1] + " " + d + ", " + yr;
dd = Date.parse(futurestring) - Date.parse(todaystring);
dday = Math.floor(dd / (60 * 60 * 1000 * 24) * 1);
dhour = Math.floor((dd % (60 * 60 * 1000 * 24)) / (60 * 60 * 1000) * 1);
dmin = Math.floor(((dd % (60 * 60 * 1000 * 24)) % (60 * 60 * 1000)) / (60 * 1000) * 1);
dsec = Math.floor((((dd % (60 * 60 * 1000 * 24)) % (60 * 60 * 1000)) % (60 * 1000)) / 1000 * 1);
if (dday < 0 && dhour < 0 && dmin < 0 && dsec < 1) {
$that.val(current);
return;
}
else
$that.val(dday + "Days, " + dhour + ":" + dmin + ":" + dsec + before);
setTimeout(function() {
$that.countdown(theyear, themonth, theday);
}, 1000);
};
</script>
<input type="text" id="6" style="width: 900px">
<script>
$("#6").countdown(2011, 7, 27);
</script>
My Question is how can i add time to this? i am not so experienced in javascript to manipulate this to work with time
var before = ""
var current = "Ended"
var montharray = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
jQuery.fn.countdown = function (yr, m, d) {
$that = $(this);
var delta = 0;
var start = function (yr, m, d) {
theyear = yr;
themonth = m;
theday = d;
var today = new Date();
var todayy = today.getYear();
if (todayy < 1000) todayy += 1900;
var todaym = today.getMonth();
var todayd = today.getDate();
var todayh = today.getHours();
var todaymin = today.getMinutes();
var todaysec = today.getSeconds();
var todaystring = montharray[todaym] + " " + todayd + ", " + todayy + " " + todayh + ":" + todaymin + ":" + todaysec
futurestring = montharray[m - 1] + " " + d + ", " + yr;
dd = Date.parse(futurestring) - Date.parse(todaystring) + delta;
dday = Math.floor(dd / (60 * 60 * 1000 * 24) * 1);
dhour = Math.floor((dd % (60 * 60 * 1000 * 24)) / (60 * 60 * 1000) * 1);
dmin = Math.floor(((dd % (60 * 60 * 1000 * 24)) % (60 * 60 * 1000)) / (60 * 1000) * 1);
dsec = Math.floor((((dd % (60 * 60 * 1000 * 24)) % (60 * 60 * 1000)) % (60 * 1000)) / 1000 * 1);
if (dday < 0 && dhour < 0 && dmin < 0 && dsec < 1) {
$that.val(current);
return;
} else $that.val(dday + "Days, " + dhour + ":" + dmin + ":" + dsec + before);
setTimeout(function () {
start(theyear, themonth, theday);
}, 1000);
}
return {
start: function () {
start(yr, m, d);
},
addTime: function (ms) {
delta += ms;
console.log(delta);
}
}
};
To use:
var cd = $("#6").countdown(2013, 7, 28);
cd.start();
cd.addTime(100000); //add time
FIDDLE
Got the solution
<script>
var before = ""
var current = "Ended"
var montharray = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
jQuery.fn.countdown = function(yr, m, d, h, min, s) {
$that = $(this);
theyear = yr;
themonth = m;
theday = d;
thehour = h;
theminute = min;
thesecond = s;
var today = new Date();
var todayy = today.getYear();
if (todayy < 1000)
todayy += 1900;
var todaym = today.getMonth();
var todayd = today.getDate();
var todayh = today.getHours();
var todaymin = today.getMinutes();
var todaysec = today.getSeconds();
var todaystring = montharray[todaym] + " " + todayd + ", " + todayy + " " + todayh + ":" + todaymin + ":" + todaysec;
futurestring = montharray[m - 1] + " " + d + ", " + yr + " " + h + ":" + min + ":" + s;
//console.log(futurestring);
dd = Date.parse(futurestring) - Date.parse(todaystring);
dday = Math.floor(dd / (60 * 60 * 1000 * 24) * 1);
dhour = Math.floor((dd % (60 * 60 * 1000 * 24)) / (60 * 60 * 1000) * 1);
dmin = Math.floor(((dd % (60 * 60 * 1000 * 24)) % (60 * 60 * 1000)) / (60 * 1000) * 1);
dsec = Math.floor((((dd % (60 * 60 * 1000 * 24)) % (60 * 60 * 1000)) % (60 * 1000)) / 1000 * 1);
if (dday < 0 && dhour < 0 && dmin < 0 && dsec < 1) {
$that.text(current);
return;
}
else {
$that.text(dday + "Days " + dhour + ":" + dmin + ":" + dsec + before);
}
setTimeout(function() {
$that.countdown(theyear, themonth, theday, thehour, theminute, thesecond);
}, 1000);
};
</script>
<input type="text" id="6" style="width: 900px">
<script>
$("#6").countdown(2011, 7, 27, 12, 13, 12);
</script>

Categories

Resources