• react倒计时功能


     

    目录

    类组件写法

    函数组件写法:

     demo: 手机获取验证码登录(验证码60秒倒计时)


    react倒计时5 秒

    React中的倒计时可以通过使用setInterval()函数来实现。下面是一个示例代码:

    类组件写法

    1. import React from 'react';
    2. import { Button } from 'antd';
    3. class A extends React.PureComponent {
    4. constructor(props){
    5. super(props)
    6. this.state = {
    7. count: 0, // 初始值0
    8. }
    9. }
    10. clickCountTime = () => {
    11. this.setState({
    12. count: 5, // 设置一个5秒倒计时
    13. })
    14. let timer = null;
    15. timer = setInterval(() => {
    16. this.setState({
    17. count: this.state.count - 1, // 倒计时减1秒
    18. }, () => {
    19. if(this.state.count < 1) {
    20. clearInterval(timer); // 清除定时器
    21. }
    22. })
    23. }, 1000)
    24. }
    25. render () {
    26. const { count } = this.state;
    27. return (
    28. <Button
    29. type='primary'
    30. style={{ width:'85px' }}
    31. disabled={!!count} // 倒计时期间不可编辑如果count=0,0是false,!0=true, !!0=false
    32. onClick={this.clickCountTime}
    33. >
    34. {count === 0 ? '获取验证码' : `${count}秒后重试`}
    35. Button>
    36. )
    37. }
    38. }
    39. export default A

     

     react倒计时60 秒

    React中的倒计时可以通过使用setInterval()函数来实现。下面是一个示例代码:

    函数组件写法:

            设置一个button按钮给点击事件,按下后状态变为disabled,开始定时器每秒减一,当时间为0时,清除定时器,重置会原来的状态。

    1. import React, { useState, useEffect, useCallback, useRef } from 'react';
    2. const CountDown () {
    3. const intervalRef = useRef(null); // 使用useRef来存储计数器的值,并在setInterval函数中访问它
    4. const [count, setCount] = useState(0); // 初始值count=0
    5. // 组件卸载时清除计时器:设置清除定时器,避免count还未为0时,组件已被Unmount
    6. useEffect(() => {
    7. return () => {
    8. clearInterval(intervalRef.current);
    9. };
    10. }, []);
    11. // 监听count的变化
    12. useEffect(() => {
    13. if (count === 59) {
    14. intervalRef.current = setInterval(() => {
    15. setCount((preCount) => preCount - 1);
    16. }, 1000);
    17. } else if (count === 0) {
    18. clearInterval(intervalRef.current);
    19. }
    20. }, [count]);
    21. // 点击事件
    22. const onGetCaptcha = useCallback(() => {
    23. setCount(59); // 从59秒开始倒计时
    24. }, []);
    25. return (
    26. <Button type='button' disabled={!!count} onClick={onGetCaptcha}>
    27. {count ? `${count} s` : '获取验证码'}
    28. Button>
    29. );
    30. };
    31. export default CountDown;

     demo: 手机获取验证码登录(验证码60秒倒计时)

    1. import { Row, Col, Input, Button } from "antd"
    2. import { useState, useRef, useEffect } from "react"
    3. const InputGroup = Input.Group;
    4. const App = () => {
    5. const [phone, setPhone] = useState(null); // 定义初始手机号
    6. const [count, setCount] = useState(0) // 默认0秒
    7. const timerRef = useRef(null) // 使用useRef设置一个倒计时器,这样我就可以防止重新渲染
    8. // 监听count的变化
    9. useEffect(() => {
    10. if (count === 59) {
    11. intervalRef.current = setInterval(() => {
    12. setCount((preCount) => preCount - 1);
    13. }, 1000);
    14. } else if (count === 0) {
    15. clearInterval(intervalRef.current);
    16. }
    17. }, [count]); // count改变就会执行
    18. // 获取验证码
    19. const getInfo = () => {
    20. const reg= /^1[3456789]\d{9}$/;
    21. if(phone !== null || phone !== underfine) {
    22. if(!reg.test(phone )) { // 若手机号不符合要求,倒计时不进行
    23. setCount(0);
    24. message.error('输入的手机号不正确!')
    25. return false;
    26. } else {
    27. // 调接口,传给后台,获取后台的status状态
    28. axios.post('', { phone }).then(res => {
    29. if(status.success === false) {
    30. setCount(0);
    31. message.error('发送失败!')
    32. } else {
    33. message.success ('发送成功!')
    34. setCount(59);
    35. }
    36. })
    37. }
    38. }
    39. // 获取手机号输入框里的值
    40. const getValue = (e) => {
    41. setPhone(e.target.value);
    42. }
    43. return (
    44. <div>
    45. <p>手机号p>
    46. <Row>
    47. <Col span={8}>
    48. <Input placeholder='请输入手机号' onBlur={e => getValue(e)} allowClear />
    49. Cow>
    50. Row>
    51. <p>短信验证码p>
    52. <Row>
    53. <Col span={8}>
    54. <InputGroup compact>
    55. <Input placeholder='请输入验证码' style={{ width: '40%'}} allowClear />
    56. <Button onClick={getInfo} disabled={!!count}>{count === 0 ? '获取验证码': `${count}秒后重发`}Button>
    57. InputGroup>
    58. Cow>
    59. Row>
    60. div>
    61. )
    62. }
    63. export default App
  • 相关阅读:
    C++官网 Tutorials C++ Language Program structure:Statements and flow control
    java主类结构
    隆云通PM100传感器
    《Unity3D高级编程之进阶主程》第一章 C#要点技术(五) 排序算法
    为什么需要森林防火气象站?
    计算机视觉传统图像处理库opencv的使用
    Kubernetes 笔记 / 入门 / 生产环境 / 用部署工具安装 Kubernetes / 用 kubeadm 启动集群 / 安装 kubeadm
    238.除自身以外数组的乘积
    nginx负载均衡+反向代理
    深入了解springmvc响应数据
  • 原文地址:https://blog.csdn.net/qq_47305413/article/details/136341727