操作步骤:
1、获取订单下单时间戳与订单限制付款时间,如:订单在60分钟会自动取消(与分钟为举例)
限制时间戳=获取订单下单时间戳+订单限制付款时间(分钟)*60;
把限制时间戳转为时间格式,方面js统一
2、用后台传过来的时间转为时间戳 -当前时间判断是否大于0,如果小于0,代表过期
注意:苹果手机不兼容时间“-”格式,要换成“/”格式,如:new Date(‘2018-09-03 15:46:13’.replace(/-/g,"/"))!!时间戳:new Date(‘2018-09-03 15:46:13’.replace(/-/g,"/")).getTime()
代码如下:
效果图:
var all_timer; //全部订单的定时器
var pay_timer; //待付款的定时器
Page({
/**
* 页面的初始数据
*/
data: {
headList: ['全部', '待付款', '待提货'],
allList: [{
createtime: "2019-12-17 15:27:51.0",
exclostime: "2020-12-17 16:27:51",
goodsdesc: "农家土鸡蛋",
goodsname: "农家土鸡蛋",
id: "od191217636",
num: "1",
orgprice: "2000.00",
pictureurl: "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1576582226487&di=4c8436594e96a02b48d6d73706d52695&imgtype=0&src=http%3A%2F%2Fbpic.588ku.com%2Felement_origin_min_pic%2F16%2F10%2F24%2F014a98260eb4f6599775e39033e31893.jpg%2521%2Ffwfh%2F804x618%2Fquality%2F90%2Funsharp%2Ftrue%2Fcompress%2Ftrue",
replacecash: "10",
state: "02",
tkamount: "500.00",
transamt: "1.00",
type: "3",
typeid: "tktg191212000"
},
{
createtime: "2019-12-17 14:16:56.0",
exclostime: "2020-12-17 15:16:56",
goodsdesc: "农家土鸡蛋",
goodsname: "农家土鸡蛋",
id: "od191217628",
num: "1",
orgprice: "2000.00",
pictureurl: "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1576582226487&di=4c8436594e96a02b48d6d73706d52695&imgtype=0&src=http%3A%2F%2Fbpic.588ku.com%2Felement_origin_min_pic%2F16%2F10%2F24%2F014a98260eb4f6599775e39033e31893.jpg%2521%2Ffwfh%2F804x618%2Fquality%2F90%2Funsharp%2Ftrue%2Fcompress%2Ftrue",
replacecash: "10",
state: "02",
tkamount: "500.00",
transamt: "1.00",
type: "3",
typeid: "tktg191212000",
},
{
createtime: "2019-12-17 14:16:48.0",
exclostime: "2019-12-17 15:16:48",
goodsdesc: "农家土鸡蛋",
goodsname: "农家土鸡蛋",
id: "od191217627",
num: "1",
orgprice: "2000.00",
pictureurl: "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1576582226487&di=4c8436594e96a02b48d6d73706d52695&imgtype=0&src=http%3A%2F%2Fbpic.588ku.com%2Felement_origin_min_pic%2F16%2F10%2F24%2F014a98260eb4f6599775e39033e31893.jpg%2521%2Ffwfh%2F804x618%2Fquality%2F90%2Funsharp%2Ftrue%2Fcompress%2Ftrue",
replacecash: "10",
state: "02",
tkamount: "500.00",
transamt: "1.00",
type: "3",
typeid: "tktg191212000",
},
{
createtime: "2019-12-17 14:16:42.0",
exclostime: "2020-12-17 15:16:42",
goodsdesc: "农家土鸡蛋",
goodsname: "农家土鸡蛋",
id: "od191217625",
num: "1",
orgprice: "2000.00",
pictureurl: "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1576582226487&di=4c8436594e96a02b48d6d73706d52695&imgtype=0&src=http%3A%2F%2Fbpic.588ku.com%2Felement_origin_min_pic%2F16%2F10%2F24%2F014a98260eb4f6599775e39033e31893.jpg%2521%2Ffwfh%2F804x618%2Fquality%2F90%2Funsharp%2Ftrue%2Fcompress%2Ftrue",
replacecash: "10",
state: "02",
tkamount: "500.00",
transamt: "1.00",
type: "3",
typeid: "tktg191212000",
},
],
payList: [{
createtime: "2019-12-17 15:27:51.0",
exclostime: "2020-12-17 16:27:51",
goodsdesc: "农家土鸡蛋",
goodsname: "农家土鸡蛋",
id: "od191217636",
num: "1",
orgprice: "2000.00",
pictureurl: "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1576582226487&di=4c8436594e96a02b48d6d73706d52695&imgtype=0&src=http%3A%2F%2Fbpic.588ku.com%2Felement_origin_min_pic%2F16%2F10%2F24%2F014a98260eb4f6599775e39033e31893.jpg%2521%2Ffwfh%2F804x618%2Fquality%2F90%2Funsharp%2Ftrue%2Fcompress%2Ftrue",
replacecash: "10",
state: "02",
tkamount: "500.00",
transamt: "1.00",
type: "3",
typeid: "tktg191212000"
},
{
createtime: "2019-12-17 14:16:56.0",
exclostime: "2020-12-17 15:16:56",
goodsdesc: "农家土鸡蛋",
goodsname: "农家土鸡蛋",
id: "od191217628",
num: "1",
orgprice: "2000.00",
pictureurl: "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1576582226487&di=4c8436594e96a02b48d6d73706d52695&imgtype=0&src=http%3A%2F%2Fbpic.588ku.com%2Felement_origin_min_pic%2F16%2F10%2F24%2F014a98260eb4f6599775e39033e31893.jpg%2521%2Ffwfh%2F804x618%2Fquality%2F90%2Funsharp%2Ftrue%2Fcompress%2Ftrue",
replacecash: "10",
state: "02",
tkamount: "500.00",
transamt: "1.00",
type: "3",
typeid: "tktg191212000"
},
],
bringList: [{
createtime: "2019-12-16 20:45:19.0",
exclostime: "2020-12-16 21:45:19",
goodsdesc: "农家土鸡蛋",
goodsname: "农家土鸡蛋",
id: "od191216577",
num: "1",
orgprice: "2000.00",
pictureurl: "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1576582226487&di=4c8436594e96a02b48d6d73706d52695&imgtype=0&src=http%3A%2F%2Fbpic.588ku.com%2Felement_origin_min_pic%2F16%2F10%2F24%2F014a98260eb4f6599775e39033e31893.jpg%2521%2Ffwfh%2F804x618%2Fquality%2F90%2Funsharp%2Ftrue%2Fcompress%2Ftrue",
replacecash: "10",
state: "03",
tkamount: "500.00",
transamt: "1.00",
type: "3",
typeid: "tktg191212000"
}, ],
actEndTimeList: [],
},
time_format: function(param) {
return param.substring(0, 4) + '-' + param.substring(4, 6) + '-' + param.substring(6, 8) + ' ' + param.substring(8, 10) + ':' + param.substring(10, 12) + ':' + param.substring(12, 14)
},
onLoad: function(options) {
var ts = this;
if (options.tabnum) {
ts.setData({
tabnum: options.tabnum
})
}
ts.allListDraw();
ts.payListDraw();
// ts.bringListDraw();
},
//所有订单页面
allListDraw() {
var ts = this;
all_timer && clearInterval(all_timer);
all_timer = setInterval(function() {
ts.changeTime(ts.data.allList, "0")
}, 1000);
},
// 待付款页面
payListDraw() {
var ts = this;
pay_timer && clearInterval(pay_timer);
pay_timer = setInterval(function() {
ts.changeTime(ts.data.payList, "1")
}, 1000);
},
changeTime: function(param, type) {
let endTimeList = [];
// 将活动的结束时间参数提成一个单独的数组,方便操作
// param.forEach(o => { endTimeList.push(o.exclostime) })
// this.setData({ actEndTimeList: endTimeList });
// 执行倒计时函数
this.countDown(param, type);
},
countDown(param, type) { //倒计时函数
// 获取当前时间,同时得到活动结束时间数组
// let newTime = new Date().getTime(); 苹果手机不兼容,换成Y/m/d H:i:s
var date = new Date();
let Y = date.getFullYear() + '/'
let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '/'
let D = date.getDate()>9?date.getDate() + ' ':'0' + date.getDate() + ' ';
let h = date.getHours()>9?date.getHours() + ':':'0' + date.getHours() + ':';
let m = date.getMinutes()>9?date.getMinutes() + ':':'0' + date.getMinutes() + ':';
let s = date.getSeconds()>9?date.getSeconds():'0' + date.getSeconds();
var newTime=Y + M + D + h + m + s;
newTime=new Date(newTime).getTime();
let endTimeList = param;
let countDownArr = [];
// 对结束时间进行处理渲染到页面
endTimeList.forEach(o => {
let endTime = new Date(o.exclostime.replace(/-/g,"/")).getTime(); //时间Y-m-d H:i:s苹果不兼容,换成Y/m/d H:i:s
let obj = null;
// 如果活动未结束,对时间进行处理
if (endTime - newTime > 0) {
let time = (endTime - newTime) / 1000;
// 获取天、时、分、秒
let day = parseInt(time / (60 * 60 * 24));
let hou = parseInt(time % (60 * 60 * 24) / 3600);
let min = parseInt(time % (60 * 60 * 24) % 3600 / 60);
let sec = parseInt(time % (60 * 60 * 24) % 3600 % 60);
o.day = this.timeFormat(day);
o.hou = this.timeFormat(hou);
o.min = this.timeFormat(min);
o.sec = this.timeFormat(sec);
} else { //活动已结束,全部设置为'00'
o.day = "00";
o.hou = "00";
o.min = "00";
o.sec = "00";
}
})
// 渲染,然后每隔一秒执行一次倒计时函数
if (type == "1") {
this.setData({
payList: endTimeList
})
} else if (type == "0") {
this.setData({
allList: endTimeList
})
}
},
timeFormat(param) { //小于10的格式化函数
return param < 10 ? '0' + param : param;
},
})
如果想要单条数据倒计时,把里面遍历取消,并且不要那么多可能的条件,后续会补充。。。。,最后感谢那位大佬的帮助,代码是学习他的,记录一下自己学习,谢谢