我收到了来自 api 的响应,该对象有一个属性 --> 秒:70。
如何从这 70 秒开始计时。我应该更改 this.setState ({})timerTime,timerStart
中的哪个属性来设置新时间(70 秒)并运行时钟?我可以使用时刻库来设置新时间。
所有代码:https://stackblitz.com/edit/react-rbv2pd
代码片段:
componentDidMount() {
this.setState({
timerTime: 70 //I get response from api {seconds:70}
})
}
startTimer = () => {
this.setState({
timerOn: true,
timerTime: this.state.timerTime,
timerStart: Date.now() - this.state.timerTime
});
this.timer = setInterval(() => {
this.setState({
timerTime: Date.now() - this.state.timerStart
});
}, 10);
};
最佳答案
您可以将从 api 获取的值转换为毫秒,然后将其分配给timerTime -
componentDidMount() {
const valueInMilliSec = valueFromApi * 1000;
this.setState({
timerTime: valueInMilliSec //I get response from api {seconds:70}
})
}
就你而言 -
this.setState({
timerTime: 70000
})
关于javascript - 如何根据 API 返回的时间再次运行时钟?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57074633/