Browse Source

repair problem: countdown will not change when target time update (#392)

dong 8 years atrás
parent
commit
002785190e
1 changed files with 9 additions and 6 deletions
  1. 9 6
      src/components/CountDown/index.js

+ 9 - 6
src/components/CountDown/index.js

@@ -21,9 +21,12 @@ class CountDown extends Component {
 
 
   componentWillReceiveProps(nextProps) {
   componentWillReceiveProps(nextProps) {
     if (this.props.target !== nextProps.target) {
     if (this.props.target !== nextProps.target) {
+      clearTimeout(this.timer);
       const { lastTime } = this.initTime(nextProps);
       const { lastTime } = this.initTime(nextProps);
       this.setState({
       this.setState({
         lastTime,
         lastTime,
+      }, () => {
+        this.tick();
       });
       });
     }
     }
   }
   }
@@ -76,18 +79,18 @@ class CountDown extends Component {
         clearTimeout(this.timer);
         clearTimeout(this.timer);
         this.setState({
         this.setState({
           lastTime: 0,
           lastTime: 0,
+        }, () => {
+          if (onEnd) {
+            onEnd();
+          }
         });
         });
-
-        if (onEnd) {
-          onEnd();
-        }
       } else {
       } else {
         lastTime -= this.interval;
         lastTime -= this.interval;
         this.setState({
         this.setState({
           lastTime,
           lastTime,
+        }, () => {
+          this.tick();
         });
         });
-
-        this.tick();
       }
       }
     }, this.interval);
     }, this.interval);
   }
   }