倒计时demo

截止到某个日期

随便设置一个截止日期:
这里写图片描述

完整代码如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Vue 测试实例 - 菜鸟教程(runoob.com)</title>
<script src="https://cdn.bootcss.com/vue/2.4.2/vue.min.js"></script>
</head>
<body>
<div id="app">
     <h3 v-if="finalMin < 0">此项挑战任务时间已截止</h3>
     <h3 v-else>
       <label>有效期倒计时</label>
       <span v-for="item in finalHr" :key="item.id">{{item}}</span>:
       <span v-for="item in finalMin" :key="item.id">{{item}}</span>
    </h3>
</div>

<script>
new Vue({
  el: '#app',
  data: function () {
    return {
      intervalId: 0,
      finalHr: 0,
      finalMin: 0,
    }
  },
    created() {
       this.intervalId = setInterval(this.countdown, 1000);
    },
  mounted: function () {
    this.countdown()
  },
  methods: {
    countdown: function () {
      let msec = (new Date('2018.09.10')-Date.now()) / 1000;
      let day =(msec / 60 / 60 / 24) | 0;
      let hr = ((msec / 60 / 60) % 24) | 0;
      let min = ((msec / 60) % 60) | 0;
      //let sec = (msec % 60) | 0;
      this.finalHr = (hr + 24 * day + '').padStart(2,'0');
      this.finalMin = ((((msec / 60) % 60) | 0) + '').padStart(2,'0');
    }
  },
 beforeDestroy() {
   clearInterval(this.intervalId);
  }
})

</script>
<style>
     h3 {
        display: block;
        font-weight: 700;
        text-align: center;
      }
     label {
          float: left;
     }
     span {
       display: inline;
       background: black;
       margin: 0 2px;
       padding: 5px 10px;
       color:#fff;
     }
</style>
</body>
</html>

倒计时60秒

这里写图片描述这里写图片描述

完整代码如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Vue 测试实例 - 菜鸟教程(runoob.com)</title>
<script src="https://cdn.bootcss.com/vue/2.4.2/vue.min.js"></script>
</head>
<body>
<div id="app">
    <button @click="send" :disabled="disabledButton">{{sendMsg == true ? time+'秒后获取':'发送'}}</button>
</div>

<script>
new Vue({
  el: '#app',
  data: function () {
    return {
      time: 60,
      disabledButton: false,
      sendMsg:false
    }
  },
  methods: {
    send: function () {
       this.disabledButton = true;
       this.sendMsg = true;
        let interval = setInterval(() => {
           if ((this.time--) <= 0) {
              this.time = 60;
              this.disabledButton = false;
              this.sendMsg = false;
              clearInterval(interval);
          }
        },1000)
    }
  },
 beforeDestroy() {
   clearInterval(this.intervalId);
  }
})

</script>
<style>
    button {
       width:90px;
       height:32px;
       font-size:15px;
       color:#fff;
       background:#64bd63;
       border:none;
       border-radius: 5px;
       outline:none;
       cursor:pointer;
    }
</style>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值