From 582c000ff7a502fa5438ec70053c2db093e31da0 Mon Sep 17 00:00:00 2001 From: Daniel Andrews Date: Fri, 20 Mar 2020 14:10:16 -0500 Subject: [PATCH 1/4] Stop timer on componentWillUnmount --- lib/timer.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/timer.js b/lib/timer.js index c65a34c..1e0bec7 100644 --- a/lib/timer.js +++ b/lib/timer.js @@ -59,6 +59,10 @@ class Timer extends Component { } } + componentWillUnmount() { + this.stop() + } + start() { const handleFinish = this.props.handleFinish ? this.props.handleFinish : () => alert("Timer Finished"); const endTime = new Date().getTime() + this.state.remainingTime; From c34f435085628a48338f6f8b9060097577a02275 Mon Sep 17 00:00:00 2001 From: Daniel Andrews Date: Fri, 20 Mar 2020 14:10:34 -0500 Subject: [PATCH 2/4] Bump package --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 84fd297..8276d3b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-native-stopwatch-timer", - "version": "0.0.21", + "version": "0.0.22", "description": "A stopwatch/timer component for React Native.", "repository": { "type": "git", @@ -17,4 +17,4 @@ "dependencies": { "prop-types": "^15.7.2" } -} +} \ No newline at end of file From 4967d38e73962ae12fa74b207321a881ebfbaa5c Mon Sep 17 00:00:00 2001 From: Daniel Andrews Date: Fri, 20 Mar 2020 15:04:50 -0500 Subject: [PATCH 3/4] Dont continue starting if already running --- lib/timer.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/timer.js b/lib/timer.js index 1e0bec7..99f33b6 100644 --- a/lib/timer.js +++ b/lib/timer.js @@ -20,6 +20,7 @@ class Timer extends Component { this.state = { started: false, remainingTime: props.totalDuration, + stopped: false, }; this.start = this.start.bind(this); this.stop = this.stop.bind(this); @@ -49,7 +50,7 @@ class Timer extends Component { componentWillReceiveProps(newProps) { - if(newProps.start) { + if (newProps.start) { this.start(); } else { this.stop(); @@ -64,6 +65,11 @@ class Timer extends Component { } start() { + if (this.interval) { + // already running, no need to re-start + return; + } + const handleFinish = this.props.handleFinish ? this.props.handleFinish : () => alert("Timer Finished"); const endTime = new Date().getTime() + this.state.remainingTime; this.interval = setInterval(() => { @@ -75,11 +81,12 @@ class Timer extends Component { return; } this.setState({remainingTime: remaining}); - }, 1); + }, 100); } stop() { clearInterval(this.interval); + this.interval = null; } reset(newDuration) { From 74185c0d1873bd0c831717f0e34839b31146b507 Mon Sep 17 00:00:00 2001 From: Daniel Andrews Date: Fri, 20 Mar 2020 15:05:27 -0500 Subject: [PATCH 4/4] Remove stopped form state; not necessary --- lib/timer.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/timer.js b/lib/timer.js index 99f33b6..2e655d7 100644 --- a/lib/timer.js +++ b/lib/timer.js @@ -20,7 +20,6 @@ class Timer extends Component { this.state = { started: false, remainingTime: props.totalDuration, - stopped: false, }; this.start = this.start.bind(this); this.stop = this.stop.bind(this);