React注冊(cè)倒計(jì)時(shí)功能的實(shí)現(xiàn)
一、React版本
16.4.1
二、具體代碼如下
設(shè)置state屬性
constructor(props){ super(props); this.state = { btnText:'獲取驗(yàn)證碼', seconds: 60, //稱(chēng)數(shù)初始化 liked: true //獲取驗(yàn)證碼文案 } }
2.倒計(jì)時(shí)
// 獲取驗(yàn)證碼 sendCode = () => { let siv = setInterval(() => { this.setState({ liked:false, seconds:this.state.seconds - 1, },() => { if(this.state.seconds == 0){ clearInterval(siv); this.setState({ liked:true, secounds:60 }) } }); },1000); }
3.jsx代碼
<FormItem {...formItemLayout} label="驗(yàn)證碼" > <Row gutter={8}> <Col span={6}> {getFieldDecorator('captcha', { rules: [{ required: true, message: '請(qǐng)輸入短信驗(yàn)證碼!' }], })( <Input /> )} </Col> <Col span={12}> <Button onClick={this.sendCode} disabled={!this.state.liked}> { this.state.liked ? <span>{this.state.btnText}</span> : <span>{this.state.seconds + ' s 后重新發(fā)送'}</span> } </Button> </Col> </Row> </FormItem>
明明很簡(jiǎn)單的,但是看網(wǎng)上有的代碼搞得很復(fù)雜一樣,當(dāng)然也可以用react相關(guān)插件,不過(guò)我覺(jué)得這樣更簡(jiǎn)潔。
ps:react 獲取服務(wù)器端時(shí)間倒計(jì)時(shí)
import React, { Component } from 'react' import axios from 'axios' export default class Timer extends Component { constructor(props) { super(props) this.state = { bool: false, days: '0', hours: '00', minutes: '00', seconds: '00' } } componentDidMount() { this.start() } async start() { this.timer && clearTimeout(this.timer) // 先清除一遍定時(shí)器,避免被調(diào)用多次 // 發(fā)起任意一個(gè)服務(wù)器請(qǐng)求, 然后從headers 里獲取服務(wù)器時(shí)間 let leftTime = await axios.get('/').then(response => { return new Date(this.props.date).getTime() - new Date(response.headers.date).getTime() // 服務(wù)器與倒計(jì)時(shí)的 時(shí)間差 }).catch(error => { console.log(error) return 0 // 這里發(fā)送的服務(wù)器請(qǐng)求失敗 設(shè)為0 }) // 倒計(jì)時(shí) this.timer = setInterval(() => { leftTime = leftTime - 1000 let { bool, days = '0', hours = '00', minutes = '00', seconds = '00' } = this.countdown(leftTime) if (bool) { // 結(jié)束倒計(jì)時(shí) clearTimeout(this.timer) } this.setState({ bool, days, hours, minutes, seconds }) }, 1000) } /** * 倒計(jì)時(shí) * @param leftTime 要倒計(jì)時(shí)的時(shí)間戳 */ countdown(leftTime) { let bool = false if (leftTime <= 0) { bool = true return { bool } } var days = parseInt(leftTime / 1000 / 60 / 60 / 24, 10) //計(jì)算剩余的天數(shù) var hours = parseInt(leftTime / 1000 / 60 / 60 % 24, 10) if (hours < 10) { hours = '0' + hours } var minutes = parseInt(leftTime / 1000 / 60 % 60, 10) if (minutes < 10) { minutes = '0' + minutes } var seconds = parseInt(leftTime / 1000 % 60, 10) if (seconds < 10) { seconds = '0' + seconds } return { bool, days, hours, minutes, seconds } } componentWillUnmount() { clearTimeout(this.timer) } render() { let { bool, days, hours, minutes, seconds } = this.state return ( <div> { bool ? <div>活動(dòng)已結(jié)束</div> : <div> {days} 天 {hours} : {minutes} : {seconds} </div> } </div> ) } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 基于vue、react實(shí)現(xiàn)倒計(jì)時(shí)效果
- 使用react render props實(shí)現(xiàn)倒計(jì)時(shí)的示例代碼
- React Native驗(yàn)證碼倒計(jì)時(shí)工具類(lèi)分享
- react native中的聊天氣泡及timer封裝成的發(fā)送驗(yàn)證碼倒計(jì)時(shí)
- ReactNative短信驗(yàn)證碼倒計(jì)時(shí)控件的實(shí)現(xiàn)代碼
- React-Native中使用驗(yàn)證碼倒計(jì)時(shí)的按鈕實(shí)例代碼
- React倒計(jì)時(shí)功能實(shí)現(xiàn)代碼——解耦通用
相關(guān)文章
react native帶索引的城市列表組件的實(shí)例代碼
本篇文章主要介紹了react-native城市列表組件的實(shí)例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-08-08React系列useSyncExternalStore學(xué)習(xí)詳解
這篇文章主要為大家介紹了React系列useSyncExternalStore的學(xué)習(xí)及示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07React利用scheduler思想實(shí)現(xiàn)任務(wù)的打斷與恢復(fù)
這篇文章主要為大家詳細(xì)介紹了React如何利用scheduler思想實(shí)現(xiàn)任務(wù)的打斷與恢復(fù),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以參考一下2024-03-03React組件設(shè)計(jì)過(guò)程之仿抖音訂單組件
這篇文章主要介紹了React組件設(shè)計(jì)過(guò)程之仿抖音訂單組件的實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07React 中常用的幾種路由跳轉(zhuǎn)方式小結(jié)
基本路由跳轉(zhuǎn)是最常見(jiàn)的一種方式,下面介紹React 中常用的幾種路由跳轉(zhuǎn)方式,感興趣的朋友一起看看吧2023-12-12React??memo允許你的組件在?props?沒(méi)有改變的情況下跳過(guò)重新渲染的問(wèn)題記錄
使用?memo?將組件包裝起來(lái),以獲得該組件的一個(gè)?記憶化?版本,只要該組件的?props?沒(méi)有改變,這個(gè)記憶化版本就不會(huì)在其父組件重新渲染時(shí)重新渲染,這篇文章主要介紹了React??memo允許你的組件在?props?沒(méi)有改變的情況下跳過(guò)重新渲染,需要的朋友可以參考下2024-06-06