//输入绑定当前dom,传入倒计时,以及发送过后显示的文本,和发送成功后的回调函数,抛出了一个点击发送事件
import {useCallback, useRef} from 'react';
export default function useSendCode({codeDom, countTime, codeText, sendSucCallBack}) {
const codeLoading = useRef(false);
const codeTimer = useRef(null);
// 倒计时
const countDown = useCallback(() => {
codeLoading.current = true;
codeTimer.current = setInterval(() => {
try {
countTime--;
codeDom.current.innerHTML = `${countTime}S`;
if (countTime < 1) {
clearInterval(codeTimer.current);
codeDom.current.innerHTML = `${codeText}`;
codeLoading.current = false;
}
} catch (e) {
console.log(e, '[e.error]');
}
}, 1000);
}, []);
const handleSendCode = () => {
if (codeLoading.current) {
return;
}
codeLoading.current = true;
countDown();
sendSucCallBack();
};
return {
handleSendCode,
};
}
hooks封装-发送验证码
最新推荐文章于 2022-11-30 20:05:46 发布