Created
March 1, 2018 02:56
-
-
Save Jiang-Xuan/f4fd6e52d2f057d3c227a72d0d81d27e to your computer and use it in GitHub Desktop.
Using Promise get a countdown utility function.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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