const [time, setTime] = useState(60);
const [btnDisabled, setBtnDisabled] = useState(false);
const [btnContent, setBtnContent] = useState('获取短信验证码');
useEffect(() => {
clearInterval(timeChange);
return () => clearInterval(timeChange);
}, []);
useEffect(() => {
if (time > 0 && time < 60) {
setBtnContent(`${time}s后重发`);
} else {
clearInterval(timeChange);
setBtnDisabled(false);
setTime(60);
setBtnContent('获取短信验证码');
}
}, [time]);
const sendSMS = () => {
timeChange = setInterval(() => setTime(t => --t), 1000);
setBtnDisabled(true);
};
网友评论