import React from 'react';
import styles from './style/style.css'
let secondsToHms = (d) =>{
d = parseInt(d);
let h = Math.floor(d / 3600);
let m = Math.floor(d % 3600 / 60);
let s = Math.floor(d % 3600 % 60);
return (
(h > 0 ? h + ":" + (m < 10 ? "0" : "") : "") + m + ":" + (s < 10 ? "0" : "") + s
)
}
class Timer extends React.Component {
constructor(...args){
super(...args);
this.state = {
sessionName:'work',
timeLeft:25,
}
}
toggleTimer(){
let timeNumber = this.state.timeLeft;
let secs = 60 * timeNumber;
setInterval(updateTime,1000);
function updateTime(){
this.setState({ //就是这里报错,想要正确的倒计
timeLeft:secondsToHms(secs),//时效果应该怎么做
});
secs--;
}
}
render() {
return (
{this.state.sessionName}
{this.state.timeLeft}
)
}
}
export default Timer;
Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
要绑定
this(敲黑板)!箭头函数:
别名:
this作用域的问题吧,你在
setInterval(updateTime,1000);上面写个let self = this;然后把this.setState改成self.setState试试,希望能帮到你。this 作用于问题