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
    }
    

      

      

    路是自己走出来的,而不是选出来的。
  • 相关阅读:
    CodeIgniter框架对数据库查询结果进行统计
    PHP的内存回收(GC)
    使用ajax请求后端程序时,关于目标程序路径问题
    JavaScript中的各种X,Y,Width,Height
    Qt编写气体安全管理系统7-设备监控
    Qt编写气体安全管理系统6-地图监控
    Qt编写气体安全管理系统5-数据监控
    Qt编写气体安全管理系统4-通信协议
    Qt编写气体安全管理系统3-用户模块
    Qt编写气体安全管理系统2-界面框架
  • 原文地址:https://www.cnblogs.com/mo3408/p/14419917.html
Copyright © 2011-2022 走看看