Is there a way I can kill/(get rid of) a timeout in reactjs?
setTimeout(function() { //do something }.bind(this), 3000);
Upon some sort of click or action, I want to be able to completely stop and end the timeout. Is there a way to do this? thanks.
Clearing setTimeout A setTimeout timer must be cleared and handle properly, otherwise, you may experience adverse side effects in your code. To clear or cancel a timer, you call the clearTimeout(); method, passing in the timer object that you created into clearTimeout().
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 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!'
You don't actually need to use clearTimeout , you only use it if you wish to cancel the timeout you already set before it happens. It's usually more practical to use clearInterval with setInterval because setInterval usually runs indefinitely. Save this answer.
Assuming this is happening inside a component, store the timeout id so it can be cancelled later. Otherwise, you'll need to store the id somewhere else it can be accessed from later, like an external store object.
this.timeout = setTimeout(function() { // Do something this.timeout = null }.bind(this), 3000) // ...elsewhere... if (this.timeout) { clearTimeout(this.timeout) this.timeout = null }
You'll probably also want to make sure any pending timeout gets cancelled in componentWillUnmount()
too:
componentWillUnmount: function() { if (this.timeout) { clearTimeout(this.timeout) } }
If you have some UI which depends on whether or not a timeout is pending, you'll want to store the id in the appropriate component's state instead.
Since React mixins are now deprecated, here's an example of a higher order component that wraps another component to give the same functionality as described in the accepted answer. It neatly cleans up any remaining timeouts on unmount, and gives the child component an API to manage this via props.
This uses ES6 classes and component composition which is the recommended way to replace mixins in 2017.
In Timeout.jsx
import React, { Component } from 'react'; const Timeout = Composition => class _Timeout extends Component { constructor(props) { super(props); } componentWillMount () { this.timeouts = []; } setTimeout () { this.timeouts.push(setTimeout.apply(null, arguments)); } clearTimeouts () { this.timeouts.forEach(clearTimeout); } componentWillUnmount () { this.clearTimeouts(); } render () { const { timeouts, setTimeout, clearTimeouts } = this; return <Composition timeouts={timeouts} setTimeout={setTimeout} clearTimeouts={clearTimeouts} { ...this.props } /> } } export default Timeout;
In MyComponent.jsx
import React, { Component } from 'react'; import Timeout from './Timeout'; class MyComponent extends Component { constructor(props) { super(props) } componentDidMount () { // You can access methods of Timeout as they // were passed down as props. this.props.setTimeout(() => { console.log("Hey! I'm timing out!") }, 1000) } render () { return <span>Hello, world!</span> } } // Pass your component to Timeout to create the magic. export default Timeout(MyComponent);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With