【发布时间】:2021-05-31 05:33:19
【问题描述】:
我试图获取用户开始输入到他提交文本的时间间隔。当他点击文本区域时,计时器启动并以秒为单位计算(小时 * 3600 + 分钟 * 60 + 秒)。当他点击提交时,计时器结束也是如此。
现在我的问题是,当我尝试减去保存在 this.state 中的两个值时,即使我先将它们存储在 var 中或者如果我执行 this.setState(),我仍然会得到“未定义”。
overallTime(e){
this.setState({Time: this.state.StartTime - this.state.EndTime})
console.log(this.state.Time)
}
onSubmit = (e) => {
this.overallTime();
var textinput = this.state.inputValue;
var inputLength = textinput.length;
var inputTime = this.state.Time;
if(textinput === this.state.Content)
{
console.log(inputLength)
var lps = inputLength / inputTime;
this.setState({LettersPerSecond: lps})
console.log(lps)
this.setState({message: 'Your score is ' + this.state.LettersPerSecond + ' letters per second! Good job! Refresh the page for another try!'})
}
else
{
this.setState({message: 'Your input is incorrect, refresh the page and start again! Remember, you always learn from your failures, so never give up!'})
}
}
【问题讨论】:
-
你在
setState中调用setState,这似乎不对 -
我在看到另一篇关于 SO 的帖子后尝试这样做,但没有奏效。忘了改回来,现在我编辑了帖子。谢谢
-
你绑定
overallTime到组件了吗? -
是的。我需要将其称为
this.overallTime而不是this.overallTime()吗? -
你能分享剩下的代码或codeandbox吗?
标签: javascript reactjs