From c32db349d90c6f37ef4ac6c92f208a5dfe4a4bc2 Mon Sep 17 00:00:00 2001 From: Carl Kittelberger Date: Sat, 26 Aug 2017 00:43:21 +0200 Subject: [PATCH] Only update upcoming date once the current upcoming date has ran out. --- src/App.jsx | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/App.jsx b/src/App.jsx index 58d7de4..83d834c 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -45,6 +45,7 @@ class App extends React.Component { componentWillUnmount() { clearInterval(this.interval); + this.interval = null; } calculateUpcomingDate() { @@ -56,7 +57,19 @@ class App extends React.Component { } updateUpcomingDate() { - this.setState({ nextUpcomingDate: this.calculateUpcomingDate() }); + const { getNow } = this.props; + + const nextUpcomingDate = this.calculateUpcomingDate(); + + if (this.interval) { + const interval = moment.duration(nextUpcomingDate.diff(getNow())) + .asMilliseconds(); + + clearInterval(this.interval); + this.interval = setInterval(this.updateUpcomingDate.bind(this), interval); + } + + this.setState({ nextUpcomingDate }); } render() {