coundown-timer.js (2391B)
1 (function($) { 2 $.fn.countdown = function(options, callback) { 3 4 //custom 'this' selector 5 thisEl = $(this); 6 7 //array of custom settings 8 var settings = { 9 'date': null, 10 'format': null 11 }; 12 13 //append the settings array to options 14 if(options) { 15 $.extend(settings, options); 16 } 17 18 //main countdown function 19 function countdown_proc() { 20 21 eventDate = Date.parse(settings['date']) / 1000; 22 currentDate = Math.floor($.now() / 1000); 23 24 if(eventDate <= currentDate) { 25 callback.call(this); 26 clearInterval(interval); 27 } 28 29 seconds = eventDate - currentDate; 30 31 days = Math.floor(seconds / (60 * 60 * 24)); //calculate the number of days 32 seconds -= days * 60 * 60 * 24; //update the seconds variable with no. of days removed 33 34 hours = Math.floor(seconds / (60 * 60)); 35 seconds -= hours * 60 * 60; //update the seconds variable with no. of hours removed 36 37 minutes = Math.floor(seconds / 60); 38 seconds -= minutes * 60; //update the seconds variable with no. of minutes removed 39 40 //conditional Ss 41 if (days == 1) { thisEl.find(".timeRefDays").text("day"); } else { thisEl.find(".timeRefDays").text("days"); } 42 if (hours == 1) { thisEl.find(".timeRefHours").text("hour"); } else { thisEl.find(".timeRefHours").text("hours"); } 43 if (minutes == 1) { thisEl.find(".timeRefMinutes").text("minute"); } else { thisEl.find(".timeRefMinutes").text("minutes"); } 44 if (seconds == 1) { thisEl.find(".timeRefSeconds").text("second"); } else { thisEl.find(".timeRefSeconds").text("seconds"); } 45 46 //logic for the two_digits ON setting 47 if(settings['format'] == "on") { 48 days = (String(days).length >= 2) ? days : "0" + days; 49 hours = (String(hours).length >= 2) ? hours : "0" + hours; 50 minutes = (String(minutes).length >= 2) ? minutes : "0" + minutes; 51 seconds = (String(seconds).length >= 2) ? seconds : "0" + seconds; 52 } 53 54 //update the countdown's html values. 55 if(!isNaN(eventDate)) { 56 thisEl.find(".days").text(days); 57 thisEl.find(".hours").text(hours); 58 thisEl.find(".minutes").text(minutes); 59 thisEl.find(".seconds").text(seconds); 60 } else { 61 alert("Invalid date. Here's an example: 12 Tuesday 2012 17:30:00"); 62 clearInterval(interval); 63 } 64 } 65 66 //run the function 67 countdown_proc(); 68 69 //loop the function 70 interval = setInterval(countdown_proc, 1000); 71 72 } 73 }) (jQuery);