基于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í)器,重置會(huì)原來的狀態(tài)。
import React, { useState, useEffect, useCallback, useRef } from 'react';
const CountDown () {
const intervalRef = useRef(null); // 使用useRef來存儲(chǔ)計(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ī)號(hào)
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改變就會(huì)執(zhí)行
// 獲取驗(yàn)證碼
const getInfo = () => {
const reg= /^1[3456789]\d{9}$/;
if(phone !== null || phone !== underfine) {
if(!reg.test(phone )) { // 若手機(jī)號(hào)不符合要求,倒計(jì)時(shí)不進(jìn)行
setCount(0);
message.error('輸入的手機(jī)號(hào)不正確!')
return false;
} else {
// 調(diào)接口,傳給后臺(tái),獲取后臺(tái)的status狀態(tài)
axios.post('', { phone }).then(res => {
if(status.success === false) {
setCount(0);
message.error('發(fā)送失敗!')
} else {
message.success ('發(fā)送成功!')
setCount(59);
}
})
}
}
// 獲取手機(jī)號(hào)輸入框里的值
const getValue = (e) => {
setPhone(e.target.value);
}
return (
<div>
<p>手機(jī)號(hào)</p>
<Row>
<Col span={8}>
<Input placeholder='請(qǐng)輸入手機(jī)號(hào)' onBlur={e => getValue(e)} allowClear />
</Cow>
</Row>
<p>短信驗(yàn)證碼</p>
<Row>
<Col span={8}>
<InputGroup compact>
<Input placeholder='請(qǐng)輸入驗(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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用VScode 插件debugger for chrome 調(diào)試react源碼的方法
這篇文章主要介紹了使用VScode 插件debugger for chrome 調(diào)試react源碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
React報(bào)錯(cuò)Too many re-renders解決
這篇文章主要為大家介紹了React報(bào)錯(cuò)Too many re-renders解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
react?express實(shí)現(xiàn)webssh?demo解析
這篇文章主要為大家介紹了詳解react?express實(shí)現(xiàn)webssh?demo解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04
React-Native之截圖組件react-native-view-shot的介紹與使用小結(jié)
這篇文章主要介紹了React-Native之截圖組件react-native-view-shot的介紹與使用小結(jié),需本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,要的朋友可以參考下2021-08-08
react中實(shí)現(xiàn)將一個(gè)視頻流為m3u8格式的轉(zhuǎn)換
這篇文章主要介紹了react中實(shí)現(xiàn)將一個(gè)視頻流為m3u8格式的轉(zhuǎn)換方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07
React利用props的children實(shí)現(xiàn)插槽功能
React中并沒有vue中的?slot?插槽概念?不過?可以通過props.children?實(shí)現(xiàn)類似功能,本文為大家整理了實(shí)現(xiàn)的具體方,需要的可以參考一下2023-07-07
封裝一個(gè)最簡(jiǎn)單ErrorBoundary組件處理react異常
這篇文章主要介紹了一個(gè)處理react異常的ErrorBoundary組件,簡(jiǎn)單實(shí)用,代碼詳細(xì),對(duì)這個(gè)組件感興趣的朋友可以參考下2021-04-04

