栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > Web开发 > JavaScript

基于vue、react实现倒计时效果

JavaScript 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

基于vue、react实现倒计时效果

本文实例为大家分享了基于vue、react实现倒计时效果的具体代码,供大家参考,具体内容如下

Vue

方案一:俩个元素

HTML:


  

JS:

var vm = new Vue({
  el: '#example',
  data() {
   return {
    time: 60, // 发送验证码倒计时
    sendMsgDisabled: false
   }
  },
  methods: {
   send() {
    let me = this;
    me.sendMsgDisabled = true;
    let interval = window.setInterval(function() {
     if ((me.time--) <= 0) {
      me.time = 60;
      me.sendMsgDisabled = false;
      window.clearInterval(interval); //停止
     }
    }, 1000);
   }
  }
 }) 

方案二:一个元素,改变文字

HTML:


//倒计时按钮禁用:disabled="sendMsgDisabled

JS:

var vm = new Vue({
  el: '#example',
  data() {
   return {
    time: 60, // 发送验证码倒计时
    sendMsgDisabled: false //按钮可用
   }
  },
  methods: {
   time(){
      this.sendMsgDisabled= true; //按钮不可用
      let interval = window.setInterval(()=> {
 this.btnText = this.time + 's重新发送'
 if ((this.time--) <= 0) {
   this.time = 120;
   this.btnText ='发送验证码'
   this.sendMsgDisabled= false; //按钮可用
   window.clearInterval(interval);
 }
      }, 1000);
    }
 }) 

React

引用块内容

time = () => {
    this.setState({ 
      times: this.state.times-1,
      btnText: '' + this.state.times + 's重新发送)',
      // discodeBtn: false,
      clearInterval:true
    })
    var siv = setInterval(() => {
      this.setState({ 
 times: this.state.times-1,
 btnText: '' + this.state.times + 's重新发送)',
 // discodeBtn: false,
 clearInterval:true
      }, () => {
 if (this.state.times === 0) {
   clearInterval(siv);
   this.setState({ 
     times: 60,
     btnText: '发送验证码', 
     // discodeBtn: true,
     clearInterval:false,
     phone:false,
     isDisabled:false
   })
 }
      });
    }, 1000);
  };

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/74122.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号