reactjs 如何在react JS中设置按钮点击的OTP计时器?

7gs2gvoe  于 2022-12-29  发布在  React
关注(0)|答案(3)|浏览(114)

**问题:-**我做了一个模态,我正在做移动的号码验证。我想OTP计时器开始点击Send OTP按钮,但它没有发生。

我用useeffect()创建了一个计时器,但是那样的话,计时器会在页面加载的时候开始运行,但是我只想在点击Send OTP按钮的时候启动计时器,我该怎么做呢?
我正在粘贴代码沙盒链接供您参考,请指导。
编码:-https://codesandbox.io/s/goofy-hawking-wd1ze9

ibps3vxo

ibps3vxo1#

ReactJs中的按钮点击事件开始倒计时:

import React from 'react'
import { useState, useEffect } from 'react';

const Timer = (props:any) => {
    const {initialMinute = 0,initialSeconds = 0} = props;
    const [ minutes, setMinutes ] = useState(initialMinute);
    const [seconds, setSeconds ] =  useState(initialSeconds);
    useEffect(()=>{
    let myInterval = setInterval(() => {
            if (seconds > 0) {
                setSeconds(seconds - 1);
            }
            if (seconds === 0) {
                if (minutes === 0) {
                    clearInterval(myInterval)
                } else {
                    setMinutes(minutes - 1);
                    setSeconds(59);
                }
            } 
        }, 1000)
        return ()=> {
            clearInterval(myInterval);
          };
    });

    return (
        <div>
        { minutes === 0 && seconds === 0
            ? null
            : <h1> {minutes}:{seconds < 10 ?  `0${seconds}` : seconds}</h1> 
        }
        </div>
    )
}

export default Timer;
1rhkuytd

1rhkuytd2#

    • 只需使用此代码段,因为它还有助于记住超时回调。**
const [timer, setTimer] = useState(60);    
const timeOutCallback = useCallback(() => setTimer(currTimer => currTimer - 1), []);

useEffect(() => {
  timer > 0 && setTimeout(timeOutCallback, 1000);
}, [timer, timeOutCallback]);

console.log(timer);

const resetTimer = function () {
  if (!timer) {
    setTimer(60);
  }
};

在JSX中,

<Text style={styles.textLogin} onPress={resetTimer}>Resend OTP ({timer})</Text>

希望这对你或其他人有帮助。
快乐编码!

3wabscal

3wabscal3#

import React, { useEffect, useState } from "react";

function App() {
  const [otp, setOtp] = useState("");
  const [minutes, setMinutes] = useState(0);
  const [seconds, setSeconds] = useState(0);

  useEffect(() => {
    const interval = setInterval(() => {
      if (seconds > 0) {
        setSeconds(seconds - 1);
      }

      if (seconds === 0) {
        if (minutes === 0) {
          clearInterval(interval);
        } else {
          setSeconds(59);
          setMinutes(minutes - 1);
        }
      }
    }, 1000);

    return () => {
      clearInterval(interval);
    };
  });

  const sendOTP = () => {
    setMinutes(2);
    setSeconds(59);
  };
  const resendOTP = () => {
    setMinutes(2);
    setSeconds(59);
  };

  return (
    <div className="container">
      <div className="card">

        <input
          placeholder="Enter OTP"
          value={otp}
          onChange={({ target }) => {
            setOtp(target.value);
          }}
        />
        <button onClick={sendOTP}>Generate Otp </button>
        <div className="countdown-text">
          {seconds > 0 || minutes > 0 ? (
            <p>
              Time Remaining: {minutes < 10 ? `0${minutes}` : minutes}:
              {seconds < 10 ? `0${seconds}` : seconds}
            </p>
          ) : (
            <p>Didn't recieve code?</p>
          )}

          <button
            disabled={seconds > 0 || minutes > 0}
            style={{
              color: seconds > 0 || minutes > 0 ? "#DFE3E8" : "#FF5630"
            }}
            onClick={resendOTP}
          >
            Resend OTP
          </button>
        </div>

        <button class Name = "submit">SUBMIT</button>
      </div>
    </div>
  );
}

export default App;

相关问题