diff --git a/lib/timer.js b/lib/timer.js index c65a34c..2e655d7 100644 --- a/lib/timer.js +++ b/lib/timer.js @@ -49,7 +49,7 @@ class Timer extends Component { componentWillReceiveProps(newProps) { - if(newProps.start) { + if (newProps.start) { this.start(); } else { this.stop(); @@ -59,7 +59,16 @@ class Timer extends Component { } } + componentWillUnmount() { + this.stop() + } + 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(() => { @@ -71,11 +80,12 @@ class Timer extends Component { return; } this.setState({remainingTime: remaining}); - }, 1); + }, 100); } stop() { clearInterval(this.interval); + this.interval = null; } reset(newDuration) { 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