基于React實現(xiàn)倒計時功能
react倒計時5 秒
React中的倒計時可以通過使用setInterval()函數(shù)來實現(xiàn)。下面是一個示例代碼:
類組件寫法
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è)置一個5秒倒計時 }) let timer = null; timer = setInterval(() => { this.setState({ count: this.state.count - 1, // 倒計時減1秒 }, () => { if(this.state.count < 1) { clearInterval(timer); // 清除定時器 } }) }, 1000) } render () { const { count } = this.state; return ( <Button type='primary' style={{ width:'85px' }} disabled={!!count} // 倒計時期間不可編輯,如果count=0,0是false,!0=true, !!0=false onClick={this.clickCountTime} > {count === 0 ? '獲取驗證碼' : `${count}秒后重試`} </Button> ) } } export default A
react倒計時60 秒
React中的倒計時可以通過使用setInterval()函數(shù)來實現(xiàn)。下面是一個示例代碼:
函數(shù)組件寫法
設(shè)置一個button按鈕給點擊事件,按下后狀態(tài)變?yōu)閐isabled,開始定時器每秒減一,當(dāng)時間為0時,清除定時器,重置會原來的狀態(tài)。
import React, { useState, useEffect, useCallback, useRef } from 'react'; const CountDown () { const intervalRef = useRef(null); // 使用useRef來存儲計數(shù)器的值,并在setInterval函數(shù)中訪問它 const [count, setCount] = useState(0); // 初始值count=0 // 組件卸載時清除計時器:設(shè)置清除定時器,避免count還未為0時,組件已被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]); // 點擊事件 const onGetCaptcha = useCallback(() => { setCount(59); // 從59秒開始倒計時 }, []); return ( <Button type='button' disabled={!!count} onClick={onGetCaptcha}> {count ? `${count} s` : '獲取驗證碼'} </Button> ); }; export default CountDown;
demo: 手機獲取驗證碼登錄(驗證碼60秒倒計時)
import { Row, Col, Input, Button } from "antd" import { useState, useRef, useEffect } from "react" const InputGroup = Input.Group; const App = () => { const [phone, setPhone] = useState(null); // 定義初始手機號 const [count, setCount] = useState(0) // 默認0秒 const timerRef = useRef(null) // 使用useRef設(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í)行 // 獲取驗證碼 const getInfo = () => { const reg= /^1[3456789]\d{9}$/; if(phone !== null || phone !== underfine) { if(!reg.test(phone )) { // 若手機號不符合要求,倒計時不進行 setCount(0); message.error('輸入的手機號不正確!') 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); } }) } } // 獲取手機號輸入框里的值 const getValue = (e) => { setPhone(e.target.value); } return ( <div> <p>手機號</p> <Row> <Col span={8}> <Input placeholder='請輸入手機號' onBlur={e => getValue(e)} allowClear /> </Cow> </Row> <p>短信驗證碼</p> <Row> <Col span={8}> <InputGroup compact> <Input placeholder='請輸入驗證碼' style={{ width: '40%'}} allowClear /> <Button onClick={getInfo} disabled={!!count}>{count === 0 ? '獲取驗證碼': `${count}秒后重發(fā)`}</Button> </InputGroup> </Cow> </Row> </div> ) } export default App
到此這篇關(guān)于基于React實現(xiàn)倒計時功能的文章就介紹到這了,更多相關(guān)React倒計時內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用VScode 插件debugger for chrome 調(diào)試react源碼的方法
這篇文章主要介紹了使用VScode 插件debugger for chrome 調(diào)試react源碼,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09react?express實現(xiàn)webssh?demo解析
這篇文章主要為大家介紹了詳解react?express實現(xiàn)webssh?demo解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-04-04React-Native之截圖組件react-native-view-shot的介紹與使用小結(jié)
這篇文章主要介紹了React-Native之截圖組件react-native-view-shot的介紹與使用小結(jié),需本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,要的朋友可以參考下2021-08-08react中實現(xiàn)將一個視頻流為m3u8格式的轉(zhuǎn)換
這篇文章主要介紹了react中實現(xiàn)將一個視頻流為m3u8格式的轉(zhuǎn)換方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-07-07React利用props的children實現(xiàn)插槽功能
React中并沒有vue中的?slot?插槽概念?不過?可以通過props.children?實現(xiàn)類似功能,本文為大家整理了實現(xiàn)的具體方,需要的可以參考一下2023-07-07封裝一個最簡單ErrorBoundary組件處理react異常
這篇文章主要介紹了一個處理react異常的ErrorBoundary組件,簡單實用,代碼詳細,對這個組件感興趣的朋友可以參考下2021-04-04