基于React實(shí)現(xiàn)倒計(jì)時(shí)功能
react倒計(jì)時(shí)5 秒
React中的倒計(jì)時(shí)可以通過使用setInterval()函數(shù)來實(shí)現(xiàn)。下面是一個(gè)示例代碼:
類組件寫法
import React from 'react';
import { Button } from 'antd';
class A extends React.PureComponent {
constructor(props){
super(props)
this.state = {
count: 0, // 初始值0
}
}
clickCountTime = () => {
this.setState({
count: 5, // 設(shè)置一個(gè)5秒倒計(jì)時(shí)
})
let timer = null;
timer = setInterval(() => {
this.setState({
count: this.state.count - 1, // 倒計(jì)時(shí)減1秒
}, () => {
if(this.state.count < 1) {
clearInterval(timer); // 清除定時(shí)器
}
})
}, 1000)
}
render () {
const { count } = this.state;
return (
<Button
type='primary'
style={{ width:'85px' }}
disabled={!!count} // 倒計(jì)時(shí)期間不可編輯,如果count=0,0是false,!0=true, !!0=false
onClick={this.clickCountTime}
>
{count === 0 ? '獲取驗(yàn)證碼' : `${count}秒后重試`}
</Button>
)
}
}
export default A
react倒計(jì)時(shí)60 秒
React中的倒計(jì)時(shí)可以通過使用setInterval()函數(shù)來實(shí)現(xiàn)。下面是一個(gè)示例代碼:
函數(shù)組件寫法
設(shè)置一個(gè)button按鈕給點(diǎn)擊事件,按下后狀態(tài)變?yōu)閐isabled,開始定時(shí)器每秒減一,當(dāng)時(shí)間為0時(shí),清除定時(shí)器,重置會原來的狀態(tài)。
import React, { useState, useEffect, useCallback, useRef } from 'react';
const CountDown () {
const intervalRef = useRef(null); // 使用useRef來存儲計(jì)數(shù)器的值,并在setInterval函數(shù)中訪問它
const [count, setCount] = useState(0); // 初始值count=0
// 組件卸載時(shí)清除計(jì)時(shí)器:設(shè)置清除定時(shí)器,避免count還未為0時(shí),組件已被Unmount
useEffect(() => {
return () => {
clearInterval(intervalRef.current);
};
}, []);
// 監(jiān)聽count的變化
useEffect(() => {
if (count === 59) {
intervalRef.current = setInterval(() => {
setCount((preCount) => preCount - 1);
}, 1000);
} else if (count === 0) {
clearInterval(intervalRef.current);
}
}, [count]);
// 點(diǎn)擊事件
const onGetCaptcha = useCallback(() => {
setCount(59); // 從59秒開始倒計(jì)時(shí)
}, []);
return (
<Button type='button' disabled={!!count} onClick={onGetCaptcha}>
{count ? `${count} s` : '獲取驗(yàn)證碼'}
</Button>
);
};
export default CountDown;
demo: 手機(jī)獲取驗(yàn)證碼登錄(驗(yàn)證碼60秒倒計(jì)時(shí))

import { Row, Col, Input, Button } from "antd"
import { useState, useRef, useEffect } from "react"
const InputGroup = Input.Group;
const App = () => {
const [phone, setPhone] = useState(null); // 定義初始手機(jī)號
const [count, setCount] = useState(0) // 默認(rèn)0秒
const timerRef = useRef(null) // 使用useRef設(shè)置一個(gè)倒計(jì)時(shí)器,這樣我就可以防止重新渲染
// 監(jiān)聽count的變化
useEffect(() => {
if (count === 59) {
intervalRef.current = setInterval(() => {
setCount((preCount) => preCount - 1);
}, 1000);
} else if (count === 0) {
clearInterval(intervalRef.current);
}
}, [count]); // count改變就會執(zhí)行
// 獲取驗(yàn)證碼
const getInfo = () => {
const reg= /^1[3456789]\d{9}$/;
if(phone !== null || phone !== underfine) {
if(!reg.test(phone )) { // 若手機(jī)號不符合要求,倒計(jì)時(shí)不進(jìn)行
setCount(0);
message.error('輸入的手機(jī)號不正確!')
return false;
} else {
// 調(diào)接口,傳給后臺,獲取后臺的status狀態(tài)
axios.post('', { phone }).then(res => {
if(status.success === false) {
setCount(0);
message.error('發(fā)送失敗!')
} else {
message.success ('發(fā)送成功!')
setCount(59);
}
})
}
}
// 獲取手機(jī)號輸入框里的值
const getValue = (e) => {
setPhone(e.target.value);
}
return (
<div>
<p>手機(jī)號</p>
<Row>
<Col span={8}>
<Input placeholder='請輸入手機(jī)號' onBlur={e => getValue(e)} allowClear />
</Cow>
</Row>
<p>短信驗(yàn)證碼</p>
<Row>
<Col span={8}>
<InputGroup compact>
<Input placeholder='請輸入驗(yàn)證碼' style={{ width: '40%'}} allowClear />
<Button onClick={getInfo} disabled={!!count}>{count === 0 ? '獲取驗(yàn)證碼': `${count}秒后重發(fā)`}</Button>
</InputGroup>
</Cow>
</Row>
</div>
)
}
export default App到此這篇關(guān)于基于React實(shí)現(xiàn)倒計(jì)時(shí)功能的文章就介紹到這了,更多相關(guān)React倒計(jì)時(shí)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
可定制react18 input otp 一次性密碼輸入組件
這篇文章主要為大家介紹了可定制react18 input otp 一次性密碼輸入組件,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
react中history(push,go,replace)切換路由方法的區(qū)別及說明
這篇文章主要介紹了react中history(push,go,replace)切換路由方法的區(qū)別及說明,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10
React.memo?React.useMemo對項(xiàng)目性能優(yōu)化使用詳解
這篇文章主要為大家介紹了React.memo?React.useMemo對項(xiàng)目性能優(yōu)化的使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
create-react-app安裝出錯(cuò)問題解決方法
這篇文章主要介紹了create-react-app安裝出錯(cuò)問題解決方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-09-09
使用React hook實(shí)現(xiàn)remember me功能
相信大家在使用 React 寫頁面的時(shí)候都遇到過完成 Remember me 的需求吧!本文就將這個(gè)需求封裝在一個(gè) React hook 中以供后續(xù)的使用,覺得有用的同學(xué)可以收藏起來以備不時(shí)之需,感興趣的小伙伴跟著小編一起來看看吧2024-04-04
React之防止按鈕多次點(diǎn)擊事件?重復(fù)提交
這篇文章主要介紹了React之防止按鈕多次點(diǎn)擊事件?重復(fù)提交問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10

