@hyunseob/countdown
Advanced tools
Comparing version 0.1.0 to 0.1.1
@@ -8,2 +8,3 @@ declare module '@hyunseob/countdown' { | ||
} | ||
type Subscriber = (count: Count) => any; | ||
@@ -24,4 +25,4 @@ export default class Countdown { | ||
*/ | ||
public observe(subscriber: (count: Count) => any): void; | ||
public observe(subscriber: Subscriber): void; | ||
} | ||
} |
@@ -9,3 +9,10 @@ "use strict"; | ||
function Countdown(future) { | ||
var _this = this; | ||
this.future = future; | ||
this.subscribers = []; | ||
setInterval(function () { | ||
_this.subscribers.forEach(function (subscriber) { | ||
return subscriber(_this.differenceFromNow()); | ||
}); | ||
}, MS_IN_SECOND); | ||
} | ||
@@ -33,6 +40,3 @@ Countdown.prototype.differenceFromNow = function () { | ||
Countdown.prototype.observe = function (subscriber) { | ||
var _this = this; | ||
setInterval(function () { | ||
subscriber(_this.differenceFromNow()); | ||
}, MS_IN_SECOND); | ||
this.subscribers.push(subscriber); | ||
}; | ||
@@ -39,0 +43,0 @@ return Countdown; |
{ | ||
"name": "@hyunseob/countdown", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "A simple, lightweight countdown module", | ||
@@ -8,3 +8,3 @@ "main": "lib/index.js", | ||
"build": "tsc", | ||
"prepublish": "npm build", | ||
"prepublish": "npm run build", | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
@@ -11,0 +11,0 @@ }, |
# countdown | ||
A simple, lightweight countdown module | ||
## Example | ||
``` javascript | ||
const countdown = new Countdown(new Date(2017, 10, 30, 23, 59, 59)); // Date must be a future. If you put the past, it will always return zero values. | ||
const $countdown = $('#countdown'); | ||
countdown.observe(({ days, hours, minutes, seconds }) => { | ||
$countdown.text(`${days}:${hours}:${minutes}:${seconds}`); | ||
}); | ||
``` |
@@ -13,5 +13,15 @@ const MS_IN_SECOND = 1000; | ||
type Subscriber = (count: Count) => any; | ||
class Countdown { | ||
constructor(private future: Date) {} | ||
private subscribers: Subscriber[] = []; | ||
constructor(private future: Date) { | ||
setInterval(() => { | ||
this.subscribers.forEach(subscriber => | ||
subscriber(this.differenceFromNow()) | ||
); | ||
}, MS_IN_SECOND); | ||
} | ||
public differenceFromNow() { | ||
@@ -41,6 +51,4 @@ const now = new Date(); | ||
public observe(subscriber: (count: Count) => any) { | ||
setInterval(() => { | ||
subscriber(this.differenceFromNow()); | ||
}, MS_IN_SECOND); | ||
public observe(subscriber: Subscriber) { | ||
this.subscribers.push(subscriber); | ||
} | ||
@@ -47,0 +55,0 @@ } |
11045
171
14