实现倒计时(VUE3)

 效果图

<view class="countdown-time">0</view><view class="countdown-time">{{countdown_days}}</view><view style="margin-right: 10rpx;font-weight: bold">天</view>
<view class="countdown-time">{{countdown_hours[0]}}</view><view class="countdown-time">{{countdown_hours[1]}}</view><view style="margin-right: 10rpx;font-weight: bold">时</view>
<view class="countdown-time">{{countdown_minutes[0]}}</view><view class="countdown-time">{{countdown_minutes[1]}}</view><view style="margin-right: 10rpx;font-weight: bold">分</view>
<view class="countdown-time">{{countdown_seconds[0]}}</view><view class="countdown-time">{{countdown_seconds[1]}}</view><view style="margin-right: 10rpx;font-weight: bold">秒</view>

 js

const countdown_days = ref(0)
const countdown_hours = ref([])
const countdown_minutes = ref([])
const countdown_seconds = ref([])
// 倒计时
const getCountdown = (drawTime) => {
  setInterval(() => {
    if(new Date().getTime() < new Date(drawTime).getTime()){
      let now = new Date().getTime()
      // let draw = new Date(drawTime).getTime()
      let draw = new Date(drawTime).getTime()
      let remain = draw - now // 现在时间与开奖时间相差毫秒数
    
      // 天数
      countdown_days.value = Math.floor(remain / 86400000)
    
      // 小时数
      let level1 = remain % 86400000
      let arr1 = Array.from(String(Math.floor(level1 / 3600000)))
      if(arr1.length < 2){
        arr1.unshift(0)
      }
      countdown_hours.value = arr1
    
      // 分钟数
      let level2 = level1 % 3600000
      let arr2 = Array.from(String(Math.floor(level2 / 60000)))
      if(arr2.length < 2){
        arr2.unshift(0)
      }
      countdown_minutes.value = arr2
    
      // 秒数
      let level3 = level2 % 60000
      let arr3 = Array.from(String(Math.floor(level3 / 1000)))
      if(arr3.length < 2){
        arr3.unshift(0)
      }
      countdown_seconds.value = arr3
    }
  }, 1000)
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值