首页 新闻 会员 周边

React setInterval定时器无法清除

0
悬赏园豆:50 [已解决问题] 解决于 2017-03-27 10:34

这是我的react代码,setinterval定时器无法清除,新手一枚,刚接触react.js,求大神帮忙看下。。

 1 <!DOCTYPE html>
 2 <html>
 3   <head>
 4     <meta charset="UTF-8" />
 5     <title>Hello React2!</title>
 6     <script src="../build/react.js"></script>
 7     <script src="../build/react-dom.js"></script>
 8     <script src="../build/browser.min.js"></script>
 9   </head>
10   <body>
11     <div id="example"></div>
12     <script type="text/babel">
13     var FatherControl = React.createClass({
14         getInitialState:function(){
15             return{
16                 isStop:false,
17                 isNum:1
18             };
19         },
20         onChildChanged:function(newState){
21             this.setState({
22                 isStop:newState     
23             });
24             var dd;
25             this.timer = setInterval(
26                 ()=>{
27                     var eeNum = this.state.isNum;
28                     this.setState({isNum:eeNum+1});
29                     if(!this.state.isStop){
30                         console.log('this is stop');
31                         console.log(this.timer); //这里可以取到这个timer的
32                         clearInterval(this.timer);
33                         this.timer = undefined;
34                     }
35                 },1000);
36         },
37         componentWillUnMount:function(){
38             clearInterval(this.timer)
39         },
40         numberAdd:function(){
41             var ee=0
42             ee = setInterval(function(){
43                          var eeNum = this.state.isNum;
44                          this.setState({isNum:eeNum+1})
45                      }.bind(this),1000);
46                     return function(){
47                         this.ee;
48                     }
49         },
50         render:function(){
51             return (
52                 <div>
53                     <ImgPanel showvalue={this.state.isNum}></ImgPanel>
54                     <MyButton callbackParent={this.onChildChanged}>Start</MyButton>
55                 </div>
56             )
57         }
58     })
59     var MyButton = React.createClass({
60         getInitialState:function(){
61             return {
62                 isStop:this.props.onclick
63             };
64         },
65         funClick:function(){
66             var newState = !this.state.isStop;
67             this.setState({
68                 isStop:newState
69             });
70             this.props.callbackParent(newState);
71         },
72         render:function(){
73             var checked = this.state.checked;
74             return(
75               <button onClick={this.funClick}>Start</button>  
76             )
77         }
78     })
79     var ImgPanel = React.createClass({
80         render:function(){
81             return(
82                 <h3>{this.props.showvalue}</h3>
83             )
84         }
85     });
86     ReactDOM.render(
87         <FatherControl/>,
88         document.getElementById('example')
89     );
90     </script>
91   </body>
92 </html>
问题补充:

现在在练习react组件部分的交互,FatherControl是父组件,MyButton和ImgPanel是子组件,ImgPanel是一个计数的功能,点击MyButton时候可以启动或者停止。刚开始练习,用的是旧版本的React...

3WLineCode的主页 3WLineCode | 初学一级 | 园豆:12
提问于:2017-03-17 13:39
< >
分享
最佳答案
0

贴一下关键部分代码、这样实现了。

3WLineCode | 初学一级 |园豆:12 | 2017-03-27 10:33
其他回答(1)
0

理解下this与作用域。在function中用this,你要看看this的指向是否正确。你代码的问题在于this指向错乱。

收获园豆:50
幻天芒 | 园豆:37175 (高人七级) | 2017-03-17 13:50

哦,好的 非常感谢,,那就应该是少了bind(this)了,麻烦再问下,需要使用闭包吗(来传递setinterval的返回值)?

支持(0) 反对(0) 3WLineCode | 园豆:12 (初学一级) | 2017-03-17 13:52

@安安静静的bug辰: 自己思考下哦,可用可不用。

支持(0) 反对(0) 幻天芒 | 园豆:37175 (高人七级) | 2017-03-17 14:29
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册