gpt4 book ai didi

javascript - 如何在reactjs中设置setInterval

转载 作者:行者123 更新时间:2023-11-29 18:52:05 26 4
gpt4 key购买 nike

<分区>

我正在尝试使用 setInterval 在 reactjs 中调用函数,如下所示:

import React,{Component} from 'react';

class Timer extends Component {

trigger(){
this.clock = this.setState({clock:Date.now()-this.props.date});
}
setInterval(this.trigger,1000);
constructor(props){
super(props);

this.state = {
clock:0
};
this.trigger = this.trigger.bind(this);
}

render(){
this.state.clock=Math.round(this.state.clock/1000);
return(
<div>
<p>You are here since</p>
<span>{this.state.clock}</span>
<p>Seconds.</p>
<button onClick={this.trigger}>Click me</button>
</div>
);
}
}

export default Timer;

它会返回此错误:

Syntax error: F:/reactjs1/project-6/timer/src/Timer.js: Unexpected token (8:22)
Syntax error: F:/reactjs1/project-6/timer/src/Timer.js: Unexpected token (8:22)

6 | this.clock = this.setState({clock:Date.now()-this.props.date});
7 | }
> 8 | setInterval(trigger,1000);
^

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com