码迷,mamicode.com
首页 > 其他好文 > 详细

react -- 计时器

时间:2016-04-28 01:47:11      阅读:363      评论:0      收藏:0      [点我收藏+]

标签:

var SetIntervalMixin = {
componentWillMount: function(){
this.intervals = [];
},
setInterval: function(){
this.intervals.push(setInterval.apply(null, arguments));
},
componentWillUnmount: function(){
this.intervals.forEach(clearInterval);
}
};

var TickTock = React.createClass({
mixins: [SetIntervalMixin],
getInitialState: function(){
return { seconds: 0 }
},
componentDidMount: function(){
this.setInterval(this.tick, 1000);
},
tick: function(){
this.setState({ seconds: this.state.seconds + 1 });
},
render: function(){
return (
<p>React has been running for {this.state.seconds} seconds.</p>
);
}
});

ReactDOM.render(
<TickTock />,
document.querySelector(‘.demo‘)
);

react -- 计时器

标签:

原文地址:http://www.cnblogs.com/lianfu/p/5441101.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!