Skip to content

Instantly share code, notes, and snippets.

@kmagiera
Created July 26, 2018 11:01
Show Gist options
  • Save kmagiera/f6276f73d7535c95294f46247e0ef27c to your computer and use it in GitHub Desktop.
Save kmagiera/f6276f73d7535c95294f46247e0ef27c to your computer and use it in GitHub Desktop.
componentDidUpdate(prevProps) {
const clockPropsHasChanged = ['hours', 'mins', 'sec', 'countdown'].some(
prop => this.props[prop] !== prevProps[prop]
);
if (clockPropsHasChanged) {
clearInterval(this._interval);
let { countdown, hours, mins, sec } = this.props;
if (countdown) {
this._interval = setInterval(() => {
sec -= 1;
if (sec < 0) {
sec = 59;
mins -= 1;
}
if (mins < 0) {
mins = 59;
hours -= 1;
}
if (hours < 0) {
clearInterval(this._interval);
} else {
this.setState({ hours, mins, sec });
}
}, 1000);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment