javascript - 如果满足条件可以清除setInterval吗?不使用 componentDidMount?

标签 javascript reactjs react-redux this

我正在创建一个简单的计时器。我希望计时器在每次达到 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/

相关文章:

javascript - 如何访问 Aurelia 中的 Window 和 Document 对象?

javascript - 如何自动聚焦于新创建的输入。 DOM

reactjs - 使用 Create React App with Relay 的最简单方法是什么?

reactjs - 在 DOM 上显示之前如何定位和调整 React Bootstrap 工具提示的大小?

javascript - 尝试更新预订时 setState 出错

reactjs - React Redux,如何处理表单项创建和重定向?

javascript - 显示数组中的数据

javascript - 对于 HTML5 视频页面的 mp4 视频,播放/暂停等视频控件未在 android chrome 上显示

javascript - 如何在 JSX 中打印 props 对象,我收到 TypeError : Cannot read property of undefined

react-native - 我可以在我的 React Native 应用程序中使用 Flutter 吗?