Skip to content

Instantly share code, notes, and snippets.

@Jiang-Xuan
Created March 1, 2018 02:56
Show Gist options
  • Save Jiang-Xuan/f4fd6e52d2f057d3c227a72d0d81d27e to your computer and use it in GitHub Desktop.
Save Jiang-Xuan/f4fd6e52d2f057d3c227a72d0d81d27e to your computer and use it in GitHub Desktop.
Using Promise get a countdown utility function.
let remain = 60;
const ct = Promise.resolve();
const countdown = (ct) => (Promise.all([new Promise(resolve => {
setTimeout(() => {
remain--;
this.setState({
getCapchacodeRemainTime: remain
});
resolve(remain);
if (remain > 0) {
ct.then(countdown(ct));
} else {
this.setState({
getCapchacodeSuccess: false,
getCapchacodeRemainTime: 0
});
}
}, 1000);
})]));
countdown(ct);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment