我将工具集合在了我自己的npm包内,此文章不再更新。
npm包地址: 渣渣宇的开发工具包
Web
H5
验证码倒计时按钮
技术栈:react-HOOK
<div className="getCode" onClick={getCode}>
{codeTxt}
</div>
const [codeTxt, setCodeTxt] = useState('获取验证码')
let timer
const getCode = () => {
if(codeTxt !== '获取验证码') return
startTime(60)
function startTime(time) {
setCodeTxt(time)
if (time === 0) {
clearTimeout(timer)
let timer2 = setTimeout(() => {
setCodeTxt('获取验证码')
clearTimeout(timer2)
}, 1000);
return
}
timer = setTimeout(() => startTime(--time), 1000)
}
}
地址栏抓参数
我一般用在react页面上,怎么用看自己
console.log(window.location.href)
// let url = window.location.href
let url = 'http://localhost:3000/?token=tokens'
if (url.slice(url.length - 2, url.length) === '#/') {
url = url.slice(0, url.length - 2)
}
let token = null
if (url.split('?')[1]) {
let params = url.split('?')[1].split('&')
for (let item of params) {
let arr = item.split('=')
switch (arr[0]) {
// 需要抓什么参数自己加
case 'token':
token = arr[1] || 0
continue
default:
continue
}
}
}
console.log(token)