Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear a settimeout in react

On each question component, I am trying to clear the time out. So on componentWillMount() I will start the timer and then on componentDidUpdate() I will clear the timeout. The timer does not seem to be working.After the timer expires I will push the user back to the home page. Any idea why the using clearTimeout() isnt working?

class Questions extends Component { 
    constructor(props){
        super(props);
        this.state ={
            error: false,
            position: null,
            redirect: false
        } 
        this.error = this.error.bind(this);   
        this.errorFalse = this.errorFalse.bind(this); 
        this.timer = this.timer.bind(this); 
    }
    timer=()=>{
        setTimeout(() => { 
            console.log('this ran') 
            this.setState({
                redirect: true
            })

    }, 5000)
    }
    componentWillMount(){
        this.setState({
            error: false
        })
        this.timer();

    }
    componentDidUpdate(prevProps, prevState, snapshot, timer){
        console.log('updated')
        clearTimeout(this.timer);
    }
like image 930
jdip88 Avatar asked Apr 11 '18 20:04

jdip88


People also ask

Can we clear setTimeout?

To cancel a setTimeout() method from running, you need to use the clearTimeout() method, passing the ID value returned when you call the setTimeout() method.

How do you set setTimeout in React?

How to use setTimeout in React. The setTimeout function accepts two arguments: the first is the callback function that we want to execute, and the second specifies the timeout in milliseconds before the function will be called. setTimeout(() => console. log('Initial timeout!'

How do I stop setTimeout interval?

Answer: Use the clearInterval() Method The setInterval() method returns an interval ID which uniquely identifies the interval. You can pass this interval ID to the global clearInterval() method to cancel or stop setInterval() call.

Should I clearTimeout React?

You only need to clear the timeout if you want the timer not to fire if it hasn't already fired. If it's already fired, or you don't care if it fires in the future, you don't need to cancel it.


2 Answers

React Hooks

useEffect(() => {
    const timer = setTimeout(() => ...some work ... , 2000);

    return () => {
      clearTimeout(timer);
    };
  });
like image 145
Milan Vukovic Avatar answered Oct 23 '22 13:10

Milan Vukovic


When you use setTimeout() it returns a timeoutID, which you use to clear the timeout.

To use it in your component, you need to store the timeoutID that was returned from the timer method:

class Questions extends Component {
  constructor(props) {
    super(props)
    this.state = {
      error: false,
      position: null,
      redirect: false
    }
    this.error = this.error.bind(this);
    this.errorFalse = this.errorFalse.bind(this);
    // this.timer = this.timer.bind(this); // don't bind an arrow function
  }
  timer = () => setTimeout(() => { // return the timeoutID
    console.log('this ran')
    this.setState({
      redirect: true
    })

  }, 5000);
  componentWillMount() {
    this.setState({
      error: false
    })
    this.timeoutID = this.timer(); // cache the timeoutID
  }
  componentDidUpdate(prevProps, prevState, snapshot, timer) {
    console.log('updated')
    clearTimeout(this.timeoutID); // clear the timeoutID
  }
like image 23
Ori Drori Avatar answered Oct 23 '22 14:10

Ori Drori