Skip to content

Commit

Permalink
Update for version 1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
iwayanwirka committed Dec 6, 2013
1 parent bbd92c1 commit a287ae6
Showing 1 changed file with 99 additions and 91 deletions.
190 changes: 99 additions & 91 deletions dscountdown.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
dsCountDown v1.0
dsCountDown v1.1
jQuery count down plugin
(c) 2013 I Wayan Wirka - http://iwayanwirka.duststone.com/dscountdown/
license: http://www.opensource.org/licenses/mit-license.php
Expand All @@ -9,188 +9,196 @@
$.fn.dsCountDown = function(givenOptions){

var ds = this;
var refreshed = 1000,
thread = null,
running = false,
left = 0,
decreament = 1,
interval = 0,
ds.data = {
refreshed : 1000,
thread : null,
running : false,
left : 0,
decreament : 1,
interval : 0,

seconds = 0,
minutes = 0,
hours = 0,
days = 0,
seconds : 0,
minutes : 0,
hours : 0,
days : 0,

elemDays= null,
elemHours= null,
elemMinutes= null,
elemSeconds= null;
elemDays: null,
elemHours: null,
elemMinutes: null,
elemSeconds: null
};


var defaults = {
startDate: new Date(), // Date Object of starting time of count down, usualy now (whether client time or given php time)
endDate: null, // Date Object of ends of count down

elemSelDays: '', // Leave blank to use default value or provide a string selector if the lement already exist, Example: .ds-days
elemSelHours: '', // Leave blank to use default value or provide a string selector if the lement already exist, Example: .ds-hours
elemSelMinutes: '', // Leave blank to use default value or provide a string selector if the lement already exist, Example: .ds-minutes
elemSelSeconds: '', // Leave blank to use default value or provide a string selector if the lement already exist, Example: .ds-seconds
elemSelDays: '', // Leave blank to use default value or provide a string selector if the lement already exist, Example: .ds-ds.data.days
elemSelHours: '', // Leave blank to use default value or provide a string selector if the lement already exist, Example: .ds-ds.data.hours
elemSelMinutes: '', // Leave blank to use default value or provide a string selector if the lement already exist, Example: .ds-ds.data.minutes
elemSelSeconds: '', // Leave blank to use default value or provide a string selector if the lement already exist, Example: .ds-ds.data.seconds

theme: 'white', // Set the theme 'white', 'black', 'red', 'flat', 'custome'

titleDays: 'Days', // Set the title of days
titleHours: 'Hours', // Set the title of hours
titleMinutes: 'Minutes', // Set the title of minutes
titleSeconds: 'Seconds', // Set the title of seconds
titleDays: 'Days', // Set the title of ds.data.days
titleHours: 'Hours', // Set the title of ds.data.hours
titleMinutes: 'Minutes', // Set the title of ds.data.minutes
titleSeconds: 'Seconds', // Set the title of ds.data.seconds

onBevoreStart: null, // callback before the count down starts
onClocking: null, // callback after the timer just clocked
onFinish: null // callback if the count down is finish or 0 timer defined
};

var options = $.extend( {}, defaults, givenOptions );
ds.options = $.extend( {}, defaults, givenOptions );

if (this.length > 1){
this.each(function() { $(this).dsCountDown(givenOptions) });
return this;
}

var init = function(){
ds.init = function(){

// Init element
if(! options.elemSelSeconds){
if(! ds.options.elemSelSeconds){
ds.prepend('<div class="ds-element ds-element-seconds">\
<div class="ds-element-title">'+ options.titleSeconds +'</div>\
<div class="ds-element-title">'+ ds.options.titleSeconds +'</div>\
<div class="ds-element-value ds-seconds">00</div>\
</div>');
elemSeconds = ds.find('.ds-seconds');
ds.data.elemSeconds = ds.find('.ds-seconds');
}else{
elemSeconds = ds.find(options.elemSelSeconds);
ds.data.elemSeconds = ds.find(ds.options.elemSelSeconds);
}

if(! options.elemSelMinutes){
if(! ds.options.elemSelMinutes){
ds.prepend('<div class="ds-element ds-element-minutes">\
<div class="ds-element-title">'+ options.titleMinutes +'</div>\
<div class="ds-element-title">'+ ds.options.titleMinutes +'</div>\
<div class="ds-element-value ds-minutes">00</div>\
</div>');
elemMinutes = ds.find('.ds-minutes');
ds.data.elemMinutes = ds.find('.ds-minutes');
}else{
elemMinutes = ds.find(options.elemSelMinutes);
ds.data.elemMinutes = ds.find(ds.options.elemSelMinutes);
}

if(! options.elemSelHours){
if(! ds.options.elemSelHours){
ds.prepend('<div class="ds-element ds-element-hours">\
<div class="ds-element-title">'+ options.titleHours +'</div>\
<div class="ds-element-title">'+ ds.options.titleHours +'</div>\
<div class="ds-element-value ds-hours">00</div>\
</div>');
elemHours = ds.find('.ds-hours');
ds.data.elemHours = ds.find('.ds-hours');
}else{
elemHours = ds.find(options.elemSelHours);
ds.data.elemHours = ds.find(ds.options.elemSelHours);
}

if(! options.elemSelDays){
if(! ds.options.elemSelDays){
ds.prepend('<div class="ds-element ds-element-days">\
<div class="ds-element-title">'+ options.titleDays +'</div>\
<div class="ds-element-title">'+ ds.options.titleDays +'</div>\
<div class="ds-element-value ds-days">00</div>\
</div>');
elemDays = ds.find('.ds-days');
ds.data.elemDays = ds.find('.ds-days');
}else{
elemDays = ds.find(options.elemSelDays);
ds.data.elemDays = ds.find(ds.options.elemSelDays);
}

ds.addClass('dsCountDown');
ds.addClass('ds-' + options.theme);
ds.addClass('ds-' + ds.options.theme);

// Init start and end
if(options.startDate && options.endDate){
interval = options.endDate.getTime() - options.startDate.getTime();
if(interval > 0){
var allSeconds = (interval / 1000);
if(ds.options.startDate && ds.options.endDate){
ds.data.interval = ds.options.endDate.getTime() - ds.options.startDate.getTime();
if(ds.data.interval > 0){
var allSeconds = (ds.data.interval / 1000);
var hoursMod = (allSeconds % 86400);
var minutesMod = (hoursMod % 3600);

left = allSeconds;
days = Math.floor(allSeconds / 86400);
hours = Math.floor(hoursMod / 3600);
minutes = Math.floor(minutesMod / 60);
seconds = Math.floor(minutesMod % 60);
ds.data.left = allSeconds;
ds.data.days = Math.floor(allSeconds / 86400);
ds.data.hours = Math.floor(hoursMod / 3600);
ds.data.minutes = Math.floor(minutesMod / 60);
ds.data.seconds = Math.floor(minutesMod % 60);
}
}

start();
ds.start();
}

var stop = function(callback){
if(running){
clearInterval(thread);
running = false;
ds.stop = function(callback){
if(ds.data.running){
clearInterval(ds.data.thread);
ds.data.running = false;
}
if(callback){
callback(ds);
}
}

var start = function(){
if(! running){

if(left > 0){
ds.start = function(){
$('#logger').append('<br/>Start');
if(! ds.data.running){
$('#logger').append('<br/>Clock');
if(ds.data.left > 0){

if(options.onBevoreStart){
options.onBevoreStart(ds);
if(ds.options.onBevoreStart){
ds.options.onBevoreStart(ds);
}

thread = setInterval(
ds.data.thread = setInterval(
function(){

if(left > 0){
if(ds.data.left > 0){

left -= decreament;
ds.data.left -= ds.data.decreament;

seconds -= decreament;
ds.data.seconds -= ds.data.decreament;

if(seconds <= 0 && (minutes > 0 || hours > 0 || days > 0)){
minutes --;
seconds = 60;
if(ds.data.seconds <= 0 && (ds.data.minutes > 0 || ds.data.hours > 0 || ds.data.days > 0)){
ds.data.minutes --;
ds.data.seconds = 60;
}

if(minutes <= 0 && (hours > 0 || days > 0)){
hours --;
minutes = 60;
if(ds.data.minutes <= 0 && (ds.data.hours > 0 || ds.data.days > 0)){
ds.data.hours --;
ds.data.minutes = 60;
}

if(hours <= 0 && days > 0){
days --;
hours = 24;
if(ds.data.hours <= 0 && ds.data.days > 0){
ds.data.days --;
ds.data.hours = 24;
}

if(elemDays)
elemDays.html((days < 10 ? '0' + days : days));
if(elemHours)
elemHours.html((hours < 10 ? '0' + hours : hours));
if(elemMinutes)
elemMinutes.html((minutes < 10 ? '0' + minutes : minutes));
if(elemSeconds)
elemSeconds.html((seconds < 10 ? '0' + seconds : seconds));
if(ds.data.elemDays)
ds.data.elemDays.html((ds.data.days < 10 ? '0' + ds.data.days : ds.data.days));
if(ds.data.elemHours)
ds.data.elemHours.html((ds.data.hours < 10 ? '0' + ds.data.hours : ds.data.hours));
if(ds.data.elemMinutes)
ds.data.elemMinutes.html((ds.data.minutes < 10 ? '0' + ds.data.minutes : ds.data.minutes));
if(ds.data.elemSeconds)
ds.data.elemSeconds.html((ds.data.seconds < 10 ? '0' + ds.data.seconds : ds.data.seconds));

if(options.onClocking){
options.onClocking(ds);
if(ds.options.onClocking){
ds.options.onClocking(ds);
}

}else{
stop(options.onFinish);
ds.stop(ds.options.onFinish);
}
},
refreshed);
running = true;
ds.data.refreshed);
ds.data.running = true;
}else{
if(options.onFinish){
options.onFinish(ds);
if(ds.options.onFinish){
ds.options.onFinish(ds);
}
}
}
}

init();
ds.init();

// var restart = function(){
// stop();
// start();
// }

}
})(jQuery);

0 comments on commit a287ae6

Please sign in to comment.