I am a beginner in the world of react-native and recently created a stop watch. However, I encountered an issue where the stop watch stops working when I navigate to another page and then return. My goal is to have the stop watch continue running until the user presses the stop button. I believe using a global variable might solve this problem, but I am unsure how to implement it. Below is my current code:
export default class Record extends Component {
constructor(props) {
super(props);
this.state = {
stopwatchStart: false,
stopWatchTime: '00:00:00'
};
toggleStopwatch() {
if (!this.state.stopwatchStart) {
startDate = new Date();
startTime = startDate.getTime();
that = this;
setInterval(function () {
var date = new Date();
time = (date.getTime() - startTime) / 1000;
hour = parseInt(time / 3600);
timeAgo = parseInt(time % 3600);
min = parseInt(timeAgo / 60);
second = parseInt(timeAgo % 60);
that.setState({
stopWatchTime: (hour + ':' + min + ':' + second)
})
}, 1000);
}
this.setState({ stopwatchStart: !this.state.stopwatchStart });}