简体   繁体   English

我如何在反应中取消setTimeout时间

[英]How can I cancel the setTimeout time in react

I have created an application in which when a button is pressed the clock time starts. 我创建了一个应用程序,其中按下按钮时时钟时间开始。 If nothing is done after 23 seconds (23000) is set to 0 and the state is changed (this.state.user) .This is correct. 如果23秒(23000)设置为0并且状态更改为(this.state.user)之后什么也不做,这是正确的。

If you click on the element again, I need the 23 seconds (23000) to be zeroed and the Timeout cleaned. 如果再次单击该元素,则需要将23秒(23000)归零并清除超时。 Because if there is still some time left in the timeout, the item closes automatically. 因为如果超时还剩一些时间,该项目将自动关闭。

I tried "clearTimeout (this.change); clearInterval (this.change); "but it doesn't work, I don't know how to cancel this time. 我尝试了“ clearTimeout(this.change); clearInterval(this.change);”,但是它不起作用,我不知道如何取消这次。

class Header extends Component {
    constructor(props) {
        super(props);

        this.change = null;

        this.state = {
            usuario: true,
            timeElapsed: 0,
            adminOrWrap: true,
        };

        ["descansoAdminWrap", "otrosDescansos",].forEach((method) => {
            this[method] = this[method].bind(this);
        });
    }

   componentDidUpdate(prevProps, prevState) {
        if (prevProps.notInCall === false && this.props.notInCall && this.state.usuario === true) {
            this.descansoAdminWrap(prevProps, prevState);
       }
   }

    descansoAdminWrap(prevProps){
        var usuarioAdminOrWrapFalse=  this.state.usuario && this.props.adminOrWrap === false;
        var notInCallFalseUsuario= this.props.notInCall === false && this.state.usuario
        //mostrar y ocultar el descanso de 'tiempo administrativo' y 'wrap time'
        if(this.state.usuario && this.props.notInCall){
            this.setState({
                usuario: false,
                timeElapsed: 0,
            });
            clearTimeout(this.change);
            clearInterval(this.change);
            if(prevProps.notInCall === false){
                this.handleAdminOrWrap(false)
                this.setState({
                    usuario: false,
                    timeElapsed: 0,
                });
                this.change = setTimeout(() => {
                    this.setState({
                        usuario: true,
                        notInCall: true,
                    });
                    this.handleAdminOrWrap(true)
                }, 23000)
            }
        }
    }

clearTimeout is used for setTimeout , and clearInterval for setInterval . clearTimeout用于setTimeoutclearInterval用于setInterval

So using a clearInterval here isn't going to help. 因此,在此处使用clearInterval将无济于事。

I would check if you are actually call the clearTimeout with a console.log inside your if condition when you expect to do so. 我会检查您是否真的希望在if条件内使用console.log调用clearTimeout Especially when you are modifying the state.usuario value to false the first time around, which would prevent you to clear the condition again. 尤其是在您第一次将state.usuario值修改为false时,这将阻止您再次清除条件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM