当前位置: 首页 > news >正文

网站上的文章经常修 内容对seo有影响吗互联网推广

网站上的文章经常修 内容对seo有影响吗,互联网推广,wordpress界面只有文字,招商网站建设全包背景 在开发过程中,经常需要使用到倒计时的场景,当用户点击后,按钮进行倒计时,然后等待邮件或者短信发送,每次都写重复代码,会让代码显得臃肿,所以封装一个组件来减少耦合 创建一个倒计时组件…

背景

在开发过程中,经常需要使用到倒计时的场景,当用户点击后,按钮进行倒计时,然后等待邮件或者短信发送,每次都写重复代码,会让代码显得臃肿,所以封装一个组件来减少耦合

创建一个倒计时组件

在这里插入图片描述

编辑基本框架

设计3个参数,一个是倒计时时长,一个是开始时执行的方法,一个是展示文本

import React, { useState, useEffect, useRef } from 'react';
import { Button } from 'antd';// 定义 CountdownButton 的属性接口
interface CountdownButtonProps extends Omit<React.ButtonHTMLAttributes<HTMLButtonElement>, 'onClick'> {countdownTime?: number;text?: string;onStart?: () => void;
}const CountdownButton: React.FC<CountdownButtonProps> = ({ countdownTime = 60, text = '获取验证码', onStart, ...restProps }) => {const [isDisabled, setIsDisabled] = useState(false);const [buttonText, setButtonText] = useState(text);// 使用useRef来保存倒计时的当前值,避免状态重置const countdownRef = useRef(countdownTime);const intervalRef = useRef<number | null>(null);return (<Button >{buttonText}</Button>);
};export default CountdownButton;

实现倒计时方法

实现剩余时间修改方法

    // 使用自定义的setCountdownRef函数来更新倒计时值const setCountdownRef = (update: (current: number) => number) => {const newCountdown = update(countdownRef.current);countdownRef.current = newCountdown;};

实现开启倒计时方法

   const handleStartCountdown = () => {// 立即更新按钮文本和状态setButtonText(`${countdownRef.current}s后重试`);setIsDisabled(true);if (typeof onStart === 'function') {onStart();}// 如果已经有定时器存在,则清除它if (intervalRef.current !== null) {clearInterval(intervalRef.current!);}intervalRef.current = setInterval(() => {setButtonText(`${countdownRef.current}s后重试`);setCountdownRef((prevCountdown) => {if (prevCountdown <= 1) {clearInterval(intervalRef.current!);intervalRef.current = null;setButtonText(text);setIsDisabled(false);return countdownTime; // 重置倒计时时间}return prevCountdown - 1;});}, 1000);

实现清楚定时器方法

    // 清除定时器useEffect(() => {return () => {if (intervalRef.current !== null) {clearInterval(intervalRef.current!);}};}, []);

完整代码

import React, { useState, useEffect, useRef } from 'react';
import { Button } from 'antd';// 定义 CountdownButton 的属性接口
interface CountdownButtonProps extends Omit<React.ButtonHTMLAttributes<HTMLButtonElement>, 'onClick'> {countdownTime?: number;text?: string;onStart?: () => void;
}const CountdownButton: React.FC<CountdownButtonProps> = ({ countdownTime = 60, text = '获取验证码', onStart, ...restProps }) => {const [isDisabled, setIsDisabled] = useState(false);const [buttonText, setButtonText] = useState(text);// 使用useRef来保存倒计时的当前值,避免状态重置const countdownRef = useRef(countdownTime);const intervalRef = useRef<number | null>(null);// 清除定时器useEffect(() => {return () => {if (intervalRef.current !== null) {clearInterval(intervalRef.current!);}};}, []);const handleStartCountdown = () => {// 立即更新按钮文本和状态setButtonText(`${countdownRef.current}s后重试`);setIsDisabled(true);if (typeof onStart === 'function') {onStart();}// 如果已经有定时器存在,则清除它if (intervalRef.current !== null) {clearInterval(intervalRef.current!);}intervalRef.current = setInterval(() => {setButtonText(`${countdownRef.current}s后重试`);setCountdownRef((prevCountdown) => {if (prevCountdown <= 1) {clearInterval(intervalRef.current!);intervalRef.current = null;setButtonText(text);setIsDisabled(false);return countdownTime; // 重置倒计时时间}return prevCountdown - 1;});}, 1000);// 立即减少一次倒计时,使首次显示正确的剩余时间setCountdownRef((prevCountdown) => prevCountdown - 1);};// 使用自定义的setCountdownRef函数来更新倒计时值const setCountdownRef = (update: (current: number) => number) => {const newCountdown = update(countdownRef.current);countdownRef.current = newCountdown;};return (<Button {...restProps} onClick={handleStartCountdown} disabled={isDisabled}>{buttonText}</Button>);
};export default CountdownButton;

使用方法

<CountdownButton countdownTime={60} text={"获取验证码"} onStart={sendMsg} type="primary" />

效果

在这里插入图片描述

http://www.sczhlp.com/news/49032/

相关文章:

  • 学校网站怎么做推广方案链接交换
  • 自己做的网站慢是什么原因新闻摘抄大全
  • 南宁软件优化网站百度关键词优化平台
  • 山东汽车行业网站开发推广互联网推广
  • 青岛做公司网站的多吗桌子seo关键词
  • 做一个简单网站多少钱免费注册二级域名的网站
  • 基情说明
  • Python金融市场的Catboost,BiLSTM,PSO预测模型实践​:电信数据,加密货币,股票数据预测
  • mysql 字符集,排序规则(utf8 utf8mb4 )
  • 删除数组某个下标的元素
  • mysql 2阶段提交(一)
  • 莱芜政府网官方网站招聘信息seo搜索排名优化方法
  • 精准软件seo是什么软件
  • 自助广告位网站源码seo推广经验
  • 免费简单网站软文营销的技巧有哪些
  • 无锡手机网站建设方案自己怎么开网站
  • 网站首页 模板简易的旅游网页制作
  • 哈尔滨手机网站制作seo平台怎么样
  • Xhell添加推广域名
  • Layui
  • 覆盖索引,索引合并,索引跳跃扫描,自适应哈希索引
  • 9.9.7 放置已分配的块
  • mysql三大日志 redolog undolog binlog
  • 做网站需要写代码吗新媒体营销六种方式
  • 南京市网站建设公司陕西seo推广
  • 企业网站管理系统 开源百度网盘官网下载
  • 射洪哪里可以做网站护肤品软文推广
  • 给别人生日做网站网站免费发布与推广
  • 【NCS随笔】添加SPI主机和SPI从机例程
  • 观察题