From 62a4c25fee8d097c2f3e363e0f9a7e65ccb45d4c Mon Sep 17 00:00:00 2001 From: ko tainen Date: Fri, 13 Mar 2020 18:21:54 +0800 Subject: [PATCH 1/2] save duration when restart --- basic_stop_watch/src/StopWatch.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/basic_stop_watch/src/StopWatch.js b/basic_stop_watch/src/StopWatch.js index 6ff838b..0245a79 100644 --- a/basic_stop_watch/src/StopWatch.js +++ b/basic_stop_watch/src/StopWatch.js @@ -28,9 +28,10 @@ class StopWatch extends React.Component { } onStart = () => { + let diff = this.state.currentTime - this.state.startTime; this.setState({ isStarted: true, - startTime: new Date(), + startTime: new Date() - diff, currentTime: new Date(), }); From 1f7d311aa288e25e17cceac8e3b471ce3fee5175 Mon Sep 17 00:00:00 2001 From: ko tainen Date: Fri, 13 Mar 2020 23:03:39 +0800 Subject: [PATCH 2/2] simplify the solution --- basic_stop_watch/src/StopWatch.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/basic_stop_watch/src/StopWatch.js b/basic_stop_watch/src/StopWatch.js index 0245a79..0908295 100644 --- a/basic_stop_watch/src/StopWatch.js +++ b/basic_stop_watch/src/StopWatch.js @@ -28,10 +28,9 @@ class StopWatch extends React.Component { } onStart = () => { - let diff = this.state.currentTime - this.state.startTime; this.setState({ isStarted: true, - startTime: new Date() - diff, + startTime: new Date() - this.state.currentTime - this.state.startTime, currentTime: new Date(), });