app.js
/** * 計算剩余時間 */ shengTime:function(options){ var t=setInterval(function(){ if(minutes<=0 || seconds<=0){ clearInterval(t) return; } var endtime=options.end_time;// 結束時間戳 var newtime=Math.floor(new Date().getTime()/1000);// 當前時間戳 var minutes=Math.floor((endtime-newtime)/60);// 分鍾數 var seconds=(endtime-newtime)%60;// 秒數 if(seconds<10){ seconds="0"+seconds; } if(minutes<10){ minutes="0"+minutes; } var sheng=minutes+":"+seconds; options.success({sheng:sheng,index:options.index})// index:訂單下標 },1000) this.globalData.time=t;// 存定時器 },
js:
var order=that.data.order_list;// 訂單列表 for(var i=0;i<res.data.data.length;i++){// 所有訂單 // 計算住幾晚 var night=(res.data.data[i].end_date - res.data.data[i].start_date)/60/60/24; // 存入住幾晚 order[i].night=night; // 計算總計 var zongji=Math.floor(res.data.data[i].room_number*res.data.data[i].price*100)/100; // 存入總計 order[i].zongji=zongji; if(res.data.data[i].status==1){// 如果為待支付訂單 var index=i; app.shengTime({//調用倒計時函數 end_time:res.data.data[index].status1_time,// 結束時間戳 index:index,// 訂單下標 success(res){ console.log(res) order[res.index].sheng=res.sheng;// 修改數組,存儲倒計時 that.setData({ order_list:order, }) } }) }
wxml:
<view class="waitpay_shengyu" wx:if="{{item.status==1}}"> <text>剩余 {{item.sheng}}</text> </view>