- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在创建一个简单的计时器。我希望计时器在每次达到 00 秒时重置。我正在使用 setInterval 但它不会在 this.state.seconds 中每次值为 0 时清除。我见过一些使用 componentDidMount 和 componentDidUnmount 的解决方案,但有兴趣从调用 setInterval 本身的方法中执行和清除 setInterval 。这是可以做的事情吗?
到目前为止,即使在 0 秒后,计时器也会继续减少。这是我的 codepen 草稿:https://codepen.io/tonytony92/pen/bGdJeRg
class MyApp extends React.Component {
constructor(props) {
super(props)
this.state = {
minutes: 25,
seconds: 59,
play: false,
display: "SESSION"
}
this.handlePlay = this.handlePlay.bind(this)
}
handlePlay() {
let func1 = () => {
this.setState({
seconds: this.state.seconds - 1
})
console.log(this.state.seconds)
}
if (this.state.seconds > 0) {
this.Myvar = setInterval(() => {
console.log(this.state.seconds)
this.setState({
seconds: this.state.seconds - 1
})
}, 200)
}
else {
console.log("minus")
clearInterval(this.Myvar) /// not clearing ///
}
}
}
最佳答案
handlePlay
在单击按钮时启动计时器,但不会按照您的预期检查剩余时间。
这是因为提供给setInterval
的函数是
console.log(this.state.seconds)
this.setState({
seconds: this.state.seconds - 1
})
而不是函数handlePlay
。
为了更好的可读性和可维护性(并且还有代码可以工作),请将处理时间减少的逻辑与启动计时器的代码分开。像这样:
class MyApp extends React.Component {
constructor(props) {
super(props)
this.state = {
minutes: 0,
seconds: 10,
play: false,
display: "SESSION"
}
this.handlePlay = this.handlePlay.bind(this);
this.handleTime = this.handleTime.bind(this);
}
// handlePlay only launches the timer.
handlePlay() {
this.Myvar = setInterval(this.handleTime, 200);
}
// handleTime deals with the logic of decrementing time.
handleTime() {
if (this.state.seconds > 0) {
console.log(this.state.seconds)
this.setState({
seconds: this.state.seconds - 1
})
} else {
console.log("minus")
clearInterval(this.Myvar)
}
}
}
handlePlay
现在是 UI(按钮单击)和逻辑(处理时间减少)之间的接口(interface)。它仅使用 setInterval
启动计时器。
handleTime
处理时间减少的逻辑。每次 setInterval
触发时都会调用它,并在时间结束后停止计时器。
关于javascript - 如果满足条件可以清除setInterval吗?不使用 componentDidMount?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60998261/
我的父渲染中有下面的代码 { this.state.OSMData.map(function(item, index) { return })
你好,当我在 componentDidMount 方法之外的任何地方控制台记录我的组件的状态时,我发现我的状态充满了空数组,而这些空数组本应被构建。我正在从 JSON 文件和 componentDid
我有多个用于注册的模式,但遇到问题。我一共有三个 Action 。首先是注册按钮,然后激活第一个注册模式以注册 google 或 facebook,然后完成模式以获取提供者无法收集的其他信息,将与预先
我构建了一个 HOC 以在我的应用程序中的 protected 路由上使用。它接收应在路由上呈现的组件,检查用户是否经过身份验证,然后呈现该组件(如果是)。它可以工作,但它会导致组件多次挂载/卸载(与
class ProductsIndex extends Component { constructor (props){ super(props); console.log(
这是我在使用 React 时经常遇到的问题。 componentDidMount 方法保证在组件第一次渲染时被触发,因此它似乎是进行 DOM 测量(如高度和偏移量)的自然位置。然而,很多时候我在组件生
预期效果:在 componentDidMount () 中,我下载 s 并将其保存在变量 timeId 中。如果 timeId 为 true,则将 this.state.timeId 传递给 load
我有一个名为 DiscardPile 的组件,它在您的 props (props.cards) 中接收如下所示的对象数组: {key: 'key', 'type', suit: 'suit', lab
我正在尝试更新 componentdidmount 中的 fetch API 调用以匹配 React 中的输入,但我很难将输入传递到 URl 的模板文字中。有人可以解释一下吗? 代码- search(
我正在使用以下方式从保存的 session 中加载数据: componentDidMount() { if (JSON.parse(localStorage.getItem('savedDat
我在这里面临并提出问题,我想要第二个意见。当我的应用程序的第一页呈现时,出于安全原因我想清除我的状态,因此在我的布局类组件中我编写: componentDidMount() { this.pr
关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。 这个问题是由于错别字或无法再重现的问题引起的。虽然类似的问题可能是on-topi
我正在调用 ajax 请求 this.props.getPostListData(this.props.lang) 并出现错误。然后我发现它是 componentDidMount 的罪魁祸首。 我的
如何修复或禁用 ESLint 以忽略 [eslint] Expected an assignment or function call and instead saw an expression. (
我正在使用连接到 API 的 redux 和 sagas 开发一个 React 应用。 有一个表单组件有两个下拉字段:一个 Program 和一个 Contact 字段。该表单设计的工作方式是,当用户
我们有一个连接到 redux 存储的组件,它具有以下 componentDidMount 定义: async componentDidMount() { const page = await fe
我有一个 Header 组件,假设在用户登录的情况下呈现他的子组件。它通过 session 存储识别条件。我试图通过控制渲染componentDidMount: renderUserHeader =
这是我第一次在这里发帖,所以我很乐意接受对我发帖方式和帖子本身的评论。我学习编程才 8 周,所以可能有很多错误。 在这种情况下,我特别需要以下代码方面的帮助。 // import React, { C
我注意到 componentDidMount 有时不会按顺序触发。 componentDidMount(){ function1(); function2(); function3(); } 我
我有一个问题: 我在 componentDidMount() 中编写了 4 个函数,但我注意到编写这些函数的顺序不受尊重。 componentDidMount() { this.checkPe
我是一名优秀的程序员,十分优秀!