uni-app 倒计时计时器

<text class="red">剩余时间:<text>{
   
   {remaining}}</text></text>
export default {
		data() {
			return {
				remaining: '', //显示剩余时间
				remainingd: ''  //数据返回时间秒
			}
		},
		onLoad() {
			uni.showLoading(); //数据加载中
			this.getmsglist();//第一次加载数据
		},
		methods: {
			//获取列表信息
			getmsglist: function() {
					var _self = this;
				_self.goodsid = uni.getStorageSync('goodsid');
				uni.request({
					url: _self._apiurl + "/app/goods/getgoodsdetail",
					method: 'POST',
					header: {
						'content-type': 'application/x-www-form-urlencoded'
					},
					data: {
						openid: _self.openid,
						token: _self.token,
					},
					success: (res) => {
						console.log(res);
						var data = res.data;
						_self.remainingd = data.data.timestamp;
						_self.jishiqi();//执行一次倒计时,因为进入页面有等待
						
						setInterval(function() {
							_self.jishiqi();
						}, 1000)

						uni.hideLoading(); //关闭加载


					}
				})
			},
			//倒计时计时器
			jishiqi: function() {
				var dj = this.remainingd;
				if (dj <= 0) {
					this.remaining = "已过期";
				} else {
					var ddf = this.djs(dj); //格式化过后的时间
					this.remaining = ddf;
					//当前时间减去时间
					dj = dj - 1;
					this.remainingd = dj;
				}

			},
			//得到的秒换算成时分秒
			djs: function(ms) { 
				var h = parseInt(ms / (60 * 60));
				var m = parseInt((ms % (60 * 60)) / 60);
				var s = (ms % (60 * 60)) % 60;
				return h + ":" + m + ":" + s;
			},
		}
	}

猜你喜欢

转载自blog.csdn.net/weixin_44285250/article/details/102516013