在订单未支付时:显示订单倒计时,15分钟后自动取消订单
倒计时结束后
思路:判断订单是否是未支付订单,是的话再倒计时;获得订单下单时间与当前时间,将当前时间减去下单时间,获得时间差time,若time大于0 ,将时间差转换成分钟和秒数的形式;
使用setInterval函数进行间歇调用,每隔1秒调用一次。
关键代码如下:
/**
* 未支付订单倒计时
*/
time: function () {
console.log('我是countDown方法')
var that = this;
/**setInterval间歇调用 */
that.data.timer = setInterval(function () {
if (that.data.static_order=='待支付'){
//订单下单时间
var buy_time = that.data.buy_time;
//计算剩余下单时间
var time = (new Date(buy_time).getTime() + 15* 60 * 1000) - (new Date().getTime());
if(time>0){
//计算剩余的分钟
var minutes = parseInt(time / 1000 / 60 % 60, 10);
//计算剩余的秒数
var seconds = parseInt(time / 1000 % 60, 10);
//判断分钟和秒数小于10要在前面加个0.
if(minutes<10){
minutes = '0' + minutes;
}
if (seconds < 10) {
seconds = '0' + seconds;
}
var timer = minutes + ":" + seconds;
that.setData({
timer: timer
})
}else{
that.setData({
static_order:'已取消',
outo:1,
})
}
}
}, 1000);
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {
clearInterval(this.data.timer);
},