Use setInterval instead of setTimeout in CaptchaField

This commit is contained in:
Alex Gleason 2020-04-28 12:09:37 -05:00
parent 642282f049
commit ffb1804c59
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7

View file

@ -28,18 +28,19 @@ class CaptchaField extends React.Component {
state = { state = {
captcha: ImmutableMap(), captcha: ImmutableMap(),
refresh: undefined,
} }
setRefreshTimeout = () => { startRefresh = () => {
const { refreshInterval } = this.props; const { refreshInterval } = this.props;
if (refreshInterval) { if (refreshInterval) {
const refreshTimeout = setTimeout(this.fetchCaptcha, refreshInterval); const refresh = setInterval(this.fetchCaptcha, refreshInterval);
this.setState({ refreshTimeout }); this.setState({ refresh });
} };
} }
clearRefreshTimeout = () => { endRefresh = () => {
clearTimeout(this.state.refreshTimeout); clearInterval(this.state.refresh);
} }
fetchCaptcha = () => { fetchCaptcha = () => {
@ -51,15 +52,15 @@ class CaptchaField extends React.Component {
}).catch(error => { }).catch(error => {
onFetchFail(error); onFetchFail(error);
}); });
this.setRefreshTimeout(); // Refresh periodically
} }
componentWillMount() { componentWillMount() {
this.fetchCaptcha(); this.fetchCaptcha();
this.startRefresh(); // Refresh periodically
} }
componentWillUnmount() { componentWillUnmount() {
this.clearRefreshTimeout(); this.endRefresh();
} }
render() { render() {