Skip to content

Instantly share code, notes, and snippets.

@haykadamyan
Created December 29, 2017 21:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save haykadamyan/216fb8b2ab27e33af1ced2065da947ad to your computer and use it in GitHub Desktop.
Save haykadamyan/216fb8b2ab27e33af1ced2065da947ad to your computer and use it in GitHub Desktop.
import React from 'react';
import moment from 'moment';
import styles from './component.pcss';
class Countdown extends React.Component {
interval = null;
componentDidMount() {
this.interval = setInterval(() => this.setState({ time: Date.now() }), 1000);
}
componentWillUnmount() {
clearInterval(this.interval);
}
render() {
const { startedAt } = this.props;
const now = moment();
const nowSeconds = now.seconds();
const startedAtSeconds = startedAt.seconds();
const dif = nowSeconds - startedAtSeconds;
const minutes = parseInt(moment.duration(startedAt.diff(now)).asMinutes());
const seconds = dif > 0 ? 60 - dif : Math.abs(dif);
return (
<div className={styles.root}>
<p>
{
`${minutes}:${seconds}`
}
</p>
</div>
);
}
}
export default Countdown;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment