class Time extends Component {
constructor(props){
super(props);
this.state={
hours:new Date().getHours(),
minute:new Date().getMinutes(),
miao:new Date().getSeconds(),
year:new Date().toLocaleDateString(),
value:'显示时间',
value2:'显示时间',
value3:'显示时间'
};
this.add=this.add.bind(this);
this.reduce=this.reduce.bind(this);
this.shuaxin=this.shuaxin.bind(this);
};
componentDidMount(){
setInterval(
()=>this.tick(),
1000
);
};
componentDidMount() {
this.timerID = setInterval(
() => this.tick(),
1000
);
}
tick(){
this.setState({
minute:new Date().getMinutes(),
miao:new Date().getSeconds(),
year:new Date().toLocaleDateString()
})
};
add(){
this.setState({
value:'增加一小时',
hours:new Date().getHours()+1
})
};
reduce(){
this.setState({
value2:'减少一小时',
hours:new Date().getHours()-1
})
};
shuaxin(){
this.setState({
value3:'重置时间',
hours:new Date().getHours()
})
};
render() {
return (
时间显示如下
{this.state.hours}:{this.state.minute}:{this.state.miao}
{this.state.value}
{this.state.value3}
{this.state.value2}
);
}
}