Creating a count down clock in typescriptRevealing store sales based on timeBasic animation in...
Plausibility of Mushroom Buildings
Outlet with 3 sets of wires
What do *foreign films* mean for an American?
MySQL importing CSV files really slow
Giving a career talk in my old university, how prominently should I tell students my salary?
Is a piano played in the same way as a harmonium?
(Codewars) Linked Lists - Remove Duplicates
Professor forcing me to attend a conference, I can't afford even with 50% funding
In the late 1940’s to early 1950’s what technology was available that could melt ice?
Does an unused member variable take up memory?
How to write a chaotic neutral protagonist and prevent my readers from thinking they are evil?
What ability score modifier does a javelin's damage use?
Is this Paypal Github SDK reference really a dangerous site?
I can't die. Who am I?
Power Strip for Europe
Signed and unsigned numbers
After `ssh` without `-X` to a machine, is it possible to change `$DISPLAY` to make it work like `ssh -X`?
From an axiomatic set theoric approach why can we take uncountable unions?
Has a sovereign Communist government ever run, and conceded loss, on a fair election?
Is it possible to find 2014 distinct positive integers whose sum is divisible by each of them?
Are small insurances worth it?
Doesn't allowing a user mode program to access kernel space memory and execute the IN and OUT instructions defeat the purpose of having CPU modes?
Called into a meeting and told we are being made redundant (laid off) and "not to share outside". Can I tell my partner?
PTIJ: Why does only a Shor Tam ask at the Seder, and not a Shor Mu'ad?
Creating a count down clock in typescript
Revealing store sales based on timeBasic animation in TypescriptTypeScript Dictionary classResource pooling in TypeScriptTypeScript JSON-Array ValidatorJavascript/Typescript anagram String searchTypeScript Generics: Create a generic mapWhich will be better declaration go to with TypeScript?Deferred Iteration in TypeScriptTypescript logger class
$begingroup$
Just of the code is, you give it a date, it will start counting down how many days, hours, minutes and seconds left by emitting an event 'countDown' and once 0 is reached 'expired' event is emitted.
How can this code be improved?
class CountDownValues {
days: number;
hours: number;
minutes: number;
seconds: number;
constructor(days: number, hours: number, minutes: number, seconds: number) {
this.days = days;
this.hours = hours;
this.minutes = minutes;
this.seconds = seconds;
}
}
class DateCountDown {
listeners: Map<string, Array<Function>>;
constructor(date: Date) {
this.startCountDown(date);
this.listeners = new Map();
this.listeners.set("countDown", []);
this.listeners.set("expired", []);
}
on(eventName: "countDown", listener: (values: CountDownValues) => void);
on(eventName: "expired", listener: (isExpired: boolean) => void);
on(eventName: string, listener: Function) {
this.listeners.get(eventName).push(listener);
}
startCountDown(date: Date) {
const thousand = 1000;
const sixty = 60;
const twentyfour = 24;
const timer = setInterval(() => {
const now = new Date().getTime();
const t = date.valueOf() - now;
const days = Math.floor(t / (thousand * sixty * sixty * twentyfour));
const hours = Math.floor((t % (thousand * sixty * sixty * twentyfour)) / (thousand * sixty * sixty));
const minutes = Math.floor((t % (thousand * sixty * sixty)) / (thousand * sixty));
const seconds = Math.floor((t % (thousand * sixty)) / thousand);
if (t <= 0) {
clearInterval(timer);
this.listeners.get("expired").forEach(listener =>
listener(true));
return;
}
this.listeners.get("countDown").forEach(listener =>
listener(new CountDownValues(days, hours, minutes, seconds))
);
}, 1000);
}
}
var date = new Date();
date.setSeconds(date.getSeconds() + 20);
let countDown = new DateCountDown(date);
countDown.on("countDown", values => {
console.log("from first", values);
});
countDown.on("expired", isExpired => {
console.log("expired:", isExpired);
});
javascript typescript
$endgroup$
add a comment |
$begingroup$
Just of the code is, you give it a date, it will start counting down how many days, hours, minutes and seconds left by emitting an event 'countDown' and once 0 is reached 'expired' event is emitted.
How can this code be improved?
class CountDownValues {
days: number;
hours: number;
minutes: number;
seconds: number;
constructor(days: number, hours: number, minutes: number, seconds: number) {
this.days = days;
this.hours = hours;
this.minutes = minutes;
this.seconds = seconds;
}
}
class DateCountDown {
listeners: Map<string, Array<Function>>;
constructor(date: Date) {
this.startCountDown(date);
this.listeners = new Map();
this.listeners.set("countDown", []);
this.listeners.set("expired", []);
}
on(eventName: "countDown", listener: (values: CountDownValues) => void);
on(eventName: "expired", listener: (isExpired: boolean) => void);
on(eventName: string, listener: Function) {
this.listeners.get(eventName).push(listener);
}
startCountDown(date: Date) {
const thousand = 1000;
const sixty = 60;
const twentyfour = 24;
const timer = setInterval(() => {
const now = new Date().getTime();
const t = date.valueOf() - now;
const days = Math.floor(t / (thousand * sixty * sixty * twentyfour));
const hours = Math.floor((t % (thousand * sixty * sixty * twentyfour)) / (thousand * sixty * sixty));
const minutes = Math.floor((t % (thousand * sixty * sixty)) / (thousand * sixty));
const seconds = Math.floor((t % (thousand * sixty)) / thousand);
if (t <= 0) {
clearInterval(timer);
this.listeners.get("expired").forEach(listener =>
listener(true));
return;
}
this.listeners.get("countDown").forEach(listener =>
listener(new CountDownValues(days, hours, minutes, seconds))
);
}, 1000);
}
}
var date = new Date();
date.setSeconds(date.getSeconds() + 20);
let countDown = new DateCountDown(date);
countDown.on("countDown", values => {
console.log("from first", values);
});
countDown.on("expired", isExpired => {
console.log("expired:", isExpired);
});
javascript typescript
$endgroup$
add a comment |
$begingroup$
Just of the code is, you give it a date, it will start counting down how many days, hours, minutes and seconds left by emitting an event 'countDown' and once 0 is reached 'expired' event is emitted.
How can this code be improved?
class CountDownValues {
days: number;
hours: number;
minutes: number;
seconds: number;
constructor(days: number, hours: number, minutes: number, seconds: number) {
this.days = days;
this.hours = hours;
this.minutes = minutes;
this.seconds = seconds;
}
}
class DateCountDown {
listeners: Map<string, Array<Function>>;
constructor(date: Date) {
this.startCountDown(date);
this.listeners = new Map();
this.listeners.set("countDown", []);
this.listeners.set("expired", []);
}
on(eventName: "countDown", listener: (values: CountDownValues) => void);
on(eventName: "expired", listener: (isExpired: boolean) => void);
on(eventName: string, listener: Function) {
this.listeners.get(eventName).push(listener);
}
startCountDown(date: Date) {
const thousand = 1000;
const sixty = 60;
const twentyfour = 24;
const timer = setInterval(() => {
const now = new Date().getTime();
const t = date.valueOf() - now;
const days = Math.floor(t / (thousand * sixty * sixty * twentyfour));
const hours = Math.floor((t % (thousand * sixty * sixty * twentyfour)) / (thousand * sixty * sixty));
const minutes = Math.floor((t % (thousand * sixty * sixty)) / (thousand * sixty));
const seconds = Math.floor((t % (thousand * sixty)) / thousand);
if (t <= 0) {
clearInterval(timer);
this.listeners.get("expired").forEach(listener =>
listener(true));
return;
}
this.listeners.get("countDown").forEach(listener =>
listener(new CountDownValues(days, hours, minutes, seconds))
);
}, 1000);
}
}
var date = new Date();
date.setSeconds(date.getSeconds() + 20);
let countDown = new DateCountDown(date);
countDown.on("countDown", values => {
console.log("from first", values);
});
countDown.on("expired", isExpired => {
console.log("expired:", isExpired);
});
javascript typescript
$endgroup$
Just of the code is, you give it a date, it will start counting down how many days, hours, minutes and seconds left by emitting an event 'countDown' and once 0 is reached 'expired' event is emitted.
How can this code be improved?
class CountDownValues {
days: number;
hours: number;
minutes: number;
seconds: number;
constructor(days: number, hours: number, minutes: number, seconds: number) {
this.days = days;
this.hours = hours;
this.minutes = minutes;
this.seconds = seconds;
}
}
class DateCountDown {
listeners: Map<string, Array<Function>>;
constructor(date: Date) {
this.startCountDown(date);
this.listeners = new Map();
this.listeners.set("countDown", []);
this.listeners.set("expired", []);
}
on(eventName: "countDown", listener: (values: CountDownValues) => void);
on(eventName: "expired", listener: (isExpired: boolean) => void);
on(eventName: string, listener: Function) {
this.listeners.get(eventName).push(listener);
}
startCountDown(date: Date) {
const thousand = 1000;
const sixty = 60;
const twentyfour = 24;
const timer = setInterval(() => {
const now = new Date().getTime();
const t = date.valueOf() - now;
const days = Math.floor(t / (thousand * sixty * sixty * twentyfour));
const hours = Math.floor((t % (thousand * sixty * sixty * twentyfour)) / (thousand * sixty * sixty));
const minutes = Math.floor((t % (thousand * sixty * sixty)) / (thousand * sixty));
const seconds = Math.floor((t % (thousand * sixty)) / thousand);
if (t <= 0) {
clearInterval(timer);
this.listeners.get("expired").forEach(listener =>
listener(true));
return;
}
this.listeners.get("countDown").forEach(listener =>
listener(new CountDownValues(days, hours, minutes, seconds))
);
}, 1000);
}
}
var date = new Date();
date.setSeconds(date.getSeconds() + 20);
let countDown = new DateCountDown(date);
countDown.on("countDown", values => {
console.log("from first", values);
});
countDown.on("expired", isExpired => {
console.log("expired:", isExpired);
});
javascript typescript
javascript typescript
asked 1 min ago
ZoinkyZoinky
1111
1111
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f215161%2fcreating-a-count-down-clock-in-typescript%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f215161%2fcreating-a-count-down-clock-in-typescript%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown