Skip to content

Instantly share code, notes, and snippets.

@brigand
Last active April 8, 2016 21:48
Show Gist options
  • Save brigand/e80cbe85432b38feb9cdb5139f4c4233 to your computer and use it in GitHub Desktop.
Save brigand/e80cbe85432b38feb9cdb5139f4c4233 to your computer and use it in GitHub Desktop.
function providesTimer(Component) {
return class Timer extends React.Component {
constructor(props) {
super(props);
this.timers = [];
this.setTimeout = this.setTimeout.bind(this);
}
setTimeout(callback, ms) {
var timerId = setTimeout(() => {
this.timers = this.timers.filter((x) => x !== timerId);
callback();
}, ms);
this.timers.push(timerId);
}
componentWillUnmount() {
this.timers.forEach((id) => clearTimeout(id));
}
render() {
return <Component {...this.props} setTimeout={this.setTimeout} />
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment