wepy 实现倒计时代码

本文介绍如何利用wepy框架在微信小程序中实现倒计时组件,包括组件引用和具体实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

倒计时组件:

<template>
  <view class='container' >{{couent}}</view>
</template>

<script>
  import wepy from 'wepy';

  export default class Daojishi extends wepy.component {
    props = {
        djstime: {
            type: Number,
            default: 'null',
            twoWay: true
        }
    };

    data = {
        couent: ''
    };

    countdown(num){
        let that = this; 
        that.couent = that.dateformat(num) 
        that.$apply()
        console.log(that.dateformat(num))
        if (num <= 0) {
            that.couent = '已经截止'
            return
        }
        setTimeout(function() {
            let newnum = num-1
            that.countdown(newnum)
        }, 1000)
    }

    dateformat(microSecond) {
        var second = Math.floor(microSecond)
        var hr = this.fillZeroPrefix(Math.floor(second / 3600))
        var min = this.fillZeroPrefix(Math.floor((second - hr * 3600) / 60))
        var sec = this.fillZeroPrefix((second - hr * 3600 - min * 60))// equal to => var sec = second % 60;
        return hr + ':' + min + ':' + sec 
    }

    fillZeroPrefix(num) {    
        return num < 10 ? '0' + num : num
    }

    onLoad() { 
        var num = this.$data.djstime
        console.log(num)
        var that = this;
        this.countdown(num)
    };

    onHide(){
        
    }

    onShow() { };
  }


</script>

组件引用:

<template>  
    <view class="container">    
         <Djs :djstime.sync="clock1"></Djs>
    </view>
</template>

<script>    
    import wepy from 'wepy'  
    import djs from '../../components/daojishi'

    export default class Time extends wepy.page {    
        config = {      
            navigationBarTitleText: '倒计时'
        }
        components = {
            Djs: djs,
        }
        onLoad() {}
        
        data = {
            clock1: 3600,
        }
    }
</script>

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值