Last active
December 17, 2015 13:38
-
-
Save klarstil/5618052 to your computer and use it in GitHub Desktop.
Simple counter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;(function ( $, window, document, undefined ) { | |
"use strict"; | |
var pluginName = 'timer', | |
defaults = { | |
'baseCls': 'timer', | |
'separator': ' ', | |
'date': new Date("6 7, 2013 10:0:0") | |
}; | |
var format = function(str) { | |
for (var i = 1; i < arguments.length; i++) { | |
str = str.replace('%' + (i - 1), arguments[i]); | |
} | |
return str; | |
}; | |
function Plugin( element, options ) { | |
this.element = element; | |
this.options = $.extend( {}, defaults, options) ; | |
this.$el = $(this.element); | |
this._defaults = defaults; | |
this._name = pluginName; | |
this.init(); | |
} | |
Plugin.prototype.init = function () { | |
var me = this, until = me.options.date; | |
me._tpl = me.createTemplate(); | |
me.refreshTemplate(); | |
window.setInterval(function() { | |
me.refreshTemplate(); | |
}, 1e3); | |
}; | |
Plugin.prototype.createTemplate = function() { | |
var me = this, sep = me.options.separator, | |
baseCls = me.options.baseCls, | |
elCls = baseCls + '-element', | |
dys, hrs, mins, secs; | |
dys = $('<span>', { | |
'class': baseCls + '-days ' + elCls, | |
'html': 0 | |
}).appendTo(me.$el); | |
if(sep && sep.length) { | |
$('<span>', { | |
'class': baseCls + '-separator ' + elCls, | |
'html': sep | |
}).appendTo(me.$el); | |
} | |
hrs = $('<span>', { | |
'class': baseCls + '-hours ' + elCls, | |
'html': 0 | |
}).appendTo(me.$el); | |
if(sep && sep.length) { | |
$('<span>', { | |
'class': baseCls + '-separator ' + elCls, | |
'html': sep | |
}).appendTo(me.$el); | |
} | |
mins = $('<span>', { | |
'class': baseCls + '-minutes ' + elCls, | |
'html': 0 | |
}).appendTo(me.$el); | |
if(sep && sep.length) { | |
$('<span>', { | |
'class': baseCls + '-separator ' + elCls, | |
'html': sep | |
}).appendTo(me.$el); | |
} | |
secs = $('<span>', { | |
'class': baseCls + '-seconds ' + elCls, | |
'html': 0 | |
}).appendTo(me.$el); | |
return { | |
'days': dys, | |
'hours': hrs, | |
'minutes': mins, | |
'seconds': secs | |
}; | |
}; | |
Plugin.prototype.refreshTemplate = function() { | |
var me = this, | |
data = me._parseDate(me.options.date), | |
tpl = me._tpl, key, time, content = ''; | |
for(key in data) { | |
// Cast to string | |
time = data[key] + ''; | |
if(time.length > 1) { | |
content = format('<span>%0</span><span>%1</span>', time.slice(0, 1), time.slice(1)); | |
} else { | |
content = format('<span>0</span><span>%1</span>', 0, time); | |
} | |
tpl[key].html(content); | |
} | |
return true; | |
}; | |
Plugin.prototype._parseDate = function(date) { | |
var now = new Date(), | |
diff = date.getTime() - now.getTime(), | |
days = 0, hrs = 0, mins = 0, secs = 0; | |
console.log(date); | |
if(diff > 0) { | |
diff = Math.floor(diff / 1e3); | |
days = Math.floor(diff / 86400); | |
diff = diff % 86400; | |
hrs = Math.floor(diff / 3600); | |
diff = diff % 3600; | |
mins = Math.floor(diff / 60); | |
diff = diff % 60; | |
secs = Math.floor(diff); | |
} | |
return { | |
'days': days, | |
'hours': hrs, | |
'minutes': mins, | |
'seconds': secs | |
}; | |
}; | |
$.fn[pluginName] = function ( options ) { | |
return this.each(function () { | |
if (!$.data(this, 'plugin_' + pluginName)) { | |
$.data(this, 'plugin_' + pluginName, | |
new Plugin( this, options )); | |
} | |
}); | |
}; | |
})( jQuery, window, document ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment