[英]Reseting timer automatically in React.js
I have a problem with restarting timer in react.js. 我在react.js中重启计时器有问题。 What I want to achieve is: when the timer reaches certain value (f.ex 20 as in my example) it restarts to 0 and automatically continues, without any event, counting again automatically.
我想要实现的是:当计时器达到某个值时(例如,在我的示例中为20),它重新启动为0,并自动继续运行,没有任何事件,自动再次计数。
Thank you in advance! 先感谢您!
Here's my code: 这是我的代码:
import React, { Component } from 'react';
require('../../sass/main.scss');
class TypeAnimation extends Component {
constructor(props) {
super(props);
this.state = {
sec: 0,
};
}
componentDidMount() {
this.textInterval = setInterval(() => {
this.setState({
sec: this.state.sec + 1
});
}, 100);
}
componentDidUpdate(prevProps, prevState) {
if (this.state.sec === 20) {
clearInterval(this.textInterval);
this.firstTimeout = setTimeout(() => {
this.setState({
sec: 0
});
}, 3000);
}
}
render() {
const inText = this.props.text[0];
const firstLine = inText.substr(0, this.state.sec);
return (
<div>
<h2>
{firstLine}
<span className='blinker'> | </span>
</h2>
</div>
);
}
}
export default TypeAnimation;
this.textInterval = setInterval(() => {
this.setState(prevState => ({
sec: prevState.sec < 20 ? prevState.sec + 1 : 0
}));
}, 1000);
PS make sure to clear interval on componentWillUnmount life cycle. PS确保清除componentWillUnmount生命周期上的间隔。
componentDidMount() {
this.textInterval = setInterval(() => {
this.setState((prevState, currentProps) => {
return {sec: prevState.sec !== 20 ? prevState.sec+1 : 0};
});
}, 1000);
}
Update the state in the timer, or if you want to wait for componentDidUpdate
, start a new timer each time from there. 更新计时器中的状态,或者如果您要等待
componentDidUpdate
,则每次从那里启动一个新计时器。 But regardless, be sure not to pass an object based on current state into setState
, that's a mistake; 但是无论如何,请确保不要将基于当前状态的对象传递给
setState
,这是一个错误; details . 细节 。 Instead, use the callback version:
而是使用回调版本:
this.setState(prevState => ({sec: (prevState.sec + 1) % 20})); // % 20 resets to 0 at 20
Here we do all the updates in the timer (no componentDidUpdate
): 在这里,我们在计时器中进行所有更新(没有
componentDidUpdate
):
class TypeAnimation extends React.Component { constructor(props) { super(props); this.state = { sec: 0, }; } componentDidMount() { this.textInterval = setInterval(() => { // Note use of the callback version, it matters this.setState(prevState => ({sec: (prevState.sec + 1) % 20})); // % 20 resets to 0 at 20 }, 100); } render() { const inText = this.props.text[0]; const firstLine = inText.substr(0, this.state.sec); return ( <div> <h2> {firstLine} <span className='blinker'> | </span> </h2> </div> ); } } ReactDOM.render( <TypeAnimation text={["abcdefghijklmnopqrstuvwxyz"]} />, document.getElementById("root") );
<div id="root"></div> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
Here we want for componentDidUpdate
before scheduling the next update (eg, setTimeout
, not setInterval
): 在这里,我们要在计划下一次更新之前使用
componentDidUpdate
(例如, setTimeout
,而不是setInterval
):
class TypeAnimation extends React.Component { constructor(props) { super(props); this.state = { sec: 0, }; } scheduleUpdate() { if (this.textTimer) { clearTimeout(this.textTimer); } this.textTimer = setTimeout(() => { // Note use of the callback version, it matters this.setState(prevState => ({sec: (prevState.sec + 1) % 20})); // % 20 resets to 0 at 20 this.textTimer = 0; }, 100); } componentDidMount() { this.scheduleUpdate(); } componentDidUpdate() { this.scheduleUpdate(); } render() { const inText = this.props.text[0]; const firstLine = inText.substr(0, this.state.sec); return ( <div> <h2> {firstLine} <span className='blinker'> | </span> </h2> </div> ); } } ReactDOM.render( <TypeAnimation text={["abcdefghijklmnopqrstuvwxyz"]} />, document.getElementById("root") );
<div id="root"></div> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.