Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions dist/jquery.countdown.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*!
* The Final Countdown for jQuery v2.2.0 (http://hilios.github.io/jQuery.countdown/)
* Copyright (c) 2016 Edson Hilios
* Copyright (c) 2017 Edson Hilios
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
Expand Down Expand Up @@ -31,7 +31,8 @@
var instances = [], matchers = [], defaultOptions = {
precision: 100,
elapse: false,
defer: false
defer: false,
start: new Date()
};
matchers.push(/^[0-9]*$/.source);
matchers.push(/([0-9]{1,2}\/){2}[0-9]{4}( [0-9]{1,2}(:[0-9]{2}){2})?/.source);
Expand Down Expand Up @@ -137,6 +138,7 @@
}
}
this.setFinalDate(finalDate);
this.setStartDate(this.options.start);
if (this.options.defer === false) {
this.start();
}
Expand Down Expand Up @@ -178,12 +180,16 @@
setFinalDate: function(value) {
this.finalDate = parseDateString(value);
},
setStartDate: function(value) {
this.startDate = parseDateString(value);
},
update: function() {
if (this.$el.closest("html").length === 0) {
this.remove();
return;
}
var now = new Date(), newTotalSecsLeft;
this.startDate.setTime(this.startDate.getTime() + this.options.precision);
var now = this.startDate, newTotalSecsLeft;
newTotalSecsLeft = this.finalDate.getTime() - now.getTime();
newTotalSecsLeft = Math.ceil(newTotalSecsLeft / 1e3);
newTotalSecsLeft = !this.options.elapse && newTotalSecsLeft < 0 ? 0 : Math.abs(newTotalSecsLeft);
Expand Down
4 changes: 2 additions & 2 deletions dist/jquery.countdown.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 11 additions & 3 deletions src/countdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
defaultOptions = {
precision: 100, // 0.1 seconds, used to update the DOM
elapse: false,
defer: false
defer: false,
start: new Date() // if not set starts from now
};
// Miliseconds
matchers.push(/^[0-9]*$/.source);
Expand Down Expand Up @@ -160,8 +161,10 @@
this.options = $.extend({}, defaultOptions, options);
}
}
// Set the final date and start
// Set the final date
this.setFinalDate(finalDate);
// Set the start date
this.setStartDate(this.options.start);
// Starts the countdown automatically unless it's defered,
// Issue #198
if (this.options.defer === false) {
Expand Down Expand Up @@ -206,13 +209,18 @@
setFinalDate: function(value) {
this.finalDate = parseDateString(value); // Cast the given date
},
setStartDate: function(value) {
this.startDate = parseDateString(value); // Cast the given date
},
update: function() {
// Stop if dom is not in the html (Thanks to @dleavitt)
if(this.$el.closest('html').length === 0) {
this.remove();
return;
}
var now = new Date(),
// Move start date by one step
this.startDate.setTime(this.startDate.getTime() + this.options.precision);
var now = this.startDate,
newTotalSecsLeft;
// Create an offset date object
newTotalSecsLeft = this.finalDate.getTime() - now.getTime(); // Millisecs
Expand Down