圆月山庄资源网 Design By www.vgjia.com
一、React版本
16.4.1
二、具体代码如下
设置state属性
constructor(props){ super(props); this.state = { btnText:'获取验证码', seconds: 60, //称数初始化 liked: true //获取验证码文案 } }
2.倒计时
// 获取验证码 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="验证码" > <Row gutter={8}> <Col span={6}> {getFieldDecorator('captcha', { rules: [{ required: true, message: '请输入短信验证码!' }], })( <Input /> )} </Col> <Col span={12}> <Button onClick={this.sendCode} disabled={!this.state.liked}> { this.state.liked "htmlcode">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) // 先清除一遍定时器,避免被调用多次 // 发起任意一个服务器请求, 然后从headers 里获取服务器时间 let leftTime = await axios.get('/').then(response => { return new Date(this.props.date).getTime() - new Date(response.headers.date).getTime() // 服务器与倒计时的 时间差 }).catch(error => { console.log(error) return 0 // 这里发送的服务器请求失败 设为0 }) // 倒计时 this.timer = setInterval(() => { leftTime = leftTime - 1000 let { bool, days = '0', hours = '00', minutes = '00', seconds = '00' } = this.countdown(leftTime) if (bool) { // 结束倒计时 clearTimeout(this.timer) } this.setState({ bool, days, hours, minutes, seconds }) }, 1000) } /** * 倒计时 * @param leftTime 要倒计时的时间戳 */ countdown(leftTime) { let bool = false if (leftTime <= 0) { bool = true return { bool } } var days = parseInt(leftTime / 1000 / 60 / 60 / 24, 10) //计算剩余的天数 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>活动已结束</div> : <div> {days} 天 {hours} : {minutes} : {seconds} </div> } </div> ) } }以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
圆月山庄资源网 Design By www.vgjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
圆月山庄资源网 Design By www.vgjia.com
暂无评论...
更新日志
2024年10月07日
2024年10月07日
- 群星《前途海量 电影原声专辑》[FLAC/分轨][227.78MB]
- 张信哲.1992-知道新曲与精丫巨石】【WAV+CUE】
- 王翠玲.1995-ANGEL【新艺宝】【WAV+CUE】
- 景冈山.1996-我的眼里只有你【大地唱片】【WAV+CUE】
- 群星《八戒 电影原声带》[320K/MP3][188.97MB]
- 群星《我的阿勒泰 影视原声带》[320K/MP3][139.47MB]
- 纪钧瀚《胎教古典音乐 钢琴与大提琴的沉浸时光》[320K/MP3][148.91MB]
- 刘雅丽.2001-丽花皇后·EMI精选王【EMI百代】【FLAC分轨】
- 齐秦.1994-黄金十年1981-1990CHINA.TOUR.LIVE精丫上华】【WAV+CUE】
- 群星.2008-本色·百代音乐人创作专辑【EMI百代】【WAV+CUE】
- 群星.2001-同步过冬AVCD【环球】【WAV+CUE】
- 群星.2020-同步过冬2020冀待晴空【环球】【WAV+CUE】
- 沈雁.1986-四季(2012梦田复刻版)【白云唱片】【WAV+CUE】
- 纪钧瀚《胎教古典音乐 钢琴与大提琴的沉浸时光》[FLAC/分轨][257.88MB]
- 《国语老歌 怀旧篇 3CD》[WAV/分轨][1.6GB]