zoukankan      html  css  js  c++  java
  • uniapp微信小程序实现倒计时功能

    1.实现的效果:

     2.js

    tuan(index) {
    				this.house_id = index;
    				var that = this;
    				var wechat = uni.getStorageSync('wechat');
    				that.wechat = wechat;
    
    				// 初始化请求
    				uni.request({
    					url: that.COMMON.ApiUrl + "/api/MakeGroup/index",
    					data: {
    						openid: wechat.openid,
    						house_id: index
    					},
    					success: (res) => {
    						console.log('拼团', res)
    						if (res.data.group['end_time']) {
    							var bend = this.getdate(res.data.group['end_time']);
    						}
    						var utils = require('../../utils/utils.js');
    						this.timer = setInterval(() => { //注意箭头函数!!
    							that.infotime = utils.getTimeLeft(bend) //使用了util.getTimeLeft
    							if (this.infotime == "0天0时0分0秒") {
    								clearInterval(this.data.timer);
    							}
    						}, 1000);
    						that.houses = res.data.houses;
    						that.group = res.data.group;
    						that.infoHtml = graceRichText.format(res.data.group['info']); //内容
    					},
    					fail: function() {
    						uni.showToast({
    							title: '接口获取失败',
    							icon: 'none',
    							duration: 500
    						});
    					}
    				})
    			},
    getdate(value) {
    if (!value) return '';
    value = parseInt(value) * 1000
    var now = new Date(value);
    var year = now.getFullYear();
    var month = now.getMonth() + 1;
    if (month < 10) {
    month = '0' + month
    }
    var date = now.getDate();
    if (date < 10) {
    date = '0' + date
    }
    return year + "-" + month + "-" + date
    },

    3.utils.js:

    //取倒计时(天时分秒)
    function getTimeLeft(datetimeTo){
      // 计算目标与现在时间差(毫秒)
      let time1 = new Date(datetimeTo).getTime();
      let time2 = new Date().getTime();
      let mss = time1 - time2;
       
      // 将时间差(毫秒)格式为:天时分秒
      let days = parseInt(mss / (1000 * 60 * 60 * 24));
      let hours = parseInt((mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
      let minutes = parseInt((mss % (1000 * 60 * 60)) / (1000 * 60));
      let seconds = parseInt((mss % (1000 * 60)) / 1000);
       
      return days + "天" + hours + "时" + minutes + "分" + seconds + "秒"
    }
     
    module.exports = {
      getTimeLeft: getTimeLeft
    }
    

      

      

    路是自己走出来的,而不是选出来的。
  • 相关阅读:
    [BJOI2019] 光线
    C# 从零开始写 SharpDx 应用 笔刷
    BAT 脚本判断当前系统是 x86 还是 x64 系统
    BAT 脚本判断当前系统是 x86 还是 x64 系统
    win2d 通过 CanvasActiveLayer 画出透明度和裁剪
    win2d 通过 CanvasActiveLayer 画出透明度和裁剪
    PowerShell 拿到显卡信息
    PowerShell 拿到显卡信息
    win10 uwp 如何使用DataTemplate
    win10 uwp 如何使用DataTemplate
  • 原文地址:https://www.cnblogs.com/mo3408/p/14419917.html
Copyright © 2011-2022 走看看