uniapp怎么实现一个页面有多个商品倒计时的效果
Admin 2022-06-23 群英技术资讯 1791 次浏览
这篇文章主要介绍“uniapp怎么实现一个页面有多个商品倒计时的效果”,有一些人在uniapp怎么实现一个页面有多个商品倒计时的效果的问题上存在疑惑,接下来小编就给大家来介绍一下相关的内容,希望对大家解答有帮助,有这个方面学习需要的朋友就继续往下看吧。本文实例为大家分享了uniapp实现一个页面多个倒计时的具体代码,供大家参考,具体内容如下
设计图(需求)

结构
<view class="group-list" v-for="item in message" :key="item.productId">
<view class="group-img" @click="navTo">
<image :src="item.productPicture"></image>
</view>
<view class="group-info">
<view class="product-name">{{ item.productName }}</view>
<view class="product-price">
<text class="discounts">¥{{ item.productCurrentPrice }}</text>
<text class="original">¥{{ item.productMarketPrice }}</text>
</view>
<view class="group-partner">
<scroll-view scroll-x>
<view class="user-img">
<view v-for="(single, index) in item.avatarList" :key="index">
<image :src="single"></image>
</view>
<view v-for="i in item.stillMissingNumber" :key="i">
<image src="../../static/ssll-img/more.png"></image>
</view>
</view>
</scroll-view>
<button open-type="share">邀请好友</button>
</view>
<view class="clock">
<text>拼团剩余:</text>
<!-- 绑定倒计时 -->
<text>{{ item.end_time1 }}</text>
</view>
</view>
</view>
js
export default {
data() {
return {
timeData: '', //存放每条数据的处理好的倒计时
timer: '', //用来清除定时器
message: [] //接口请求返回的数据
}
},
onUnload(){ //卸载页面清除定时器
clearInterval(this.timer)
},
methods: {
getTimeList(){
let that = this
that.message.forEach((item) =>{
var nowdate = new Date().getTime() //获取当前时间毫秒数
var time = item.productExpiredTime.replace(new RegExp("-", "gm"), "/") //ios不能识别日期格式中的 "-" ; .productExpiredTime是接口返回的名称
var timesp = time.split('.')[0] //此处是因为我们接口返回的时间格式是这样:"2019-12-31 11:00:00.0"
var enddate = new Date(timesp).getTime() //处理好格式之后获取结束时间的毫秒数
var totaltime = enddate - nowdate //获取时间差
that.totaltime(totaltime) //这是下面封装的方法,将毫秒数处理成"xx时xx分xx秒"
item.end_time1 = that.timeData //处理好的单个时间安排到item上(重要)
})
this.message = that.message //全部处理好的数据重新赋值
},
totaltime(a){ //计算单个剩余时间
let totaltime = a
let that = this
var h, m, s, d
function test(num) {
if (num < 10) {
num = "0" + num
}
return num
}
if (totaltime > 0) {
d = Math.floor(totaltime / 1000 / 60 / 60 / 24) * 24
h = Math.floor(totaltime / 1000 / 60 / 60 % 24)
m = Math.floor(totaltime / 1000 / 60 % 60)
s = Math.floor(totaltime / 1000 % 60)
//获取时分秒
h = d + h
h = test(h)
m = test(m)
s = test(s)
this.timeData =`${h}时 : ${m}分 : ${s}秒` // 每个时间的显示格式
} else {
this.timeData = `00 : 00 : 00`
}
},
//以下请求此页面需要的数据
getUserGroupList(接口参数) {
this.$ajax({
url: 'xxx/xxx/xxxxxx',
data: {接口参数},
success: res => {
var that = this
let data = res.data.groups
if (data.length === 0) {
this.$api.msg('暂时您还没有参团信息!')
setTimeout (function() {
uni.navigateBack({ //返回上一页
delta: 1
})
},1500)
} else {
this.message = [...that.message, ...res.data.groups] //合并
//数据返回成功之后再调计时器,防止异步
//that.getTimeList()
var timer = setInterval(function () {
that.getTimeList()
}, 1000)
this.timer = timer
}
}
}
}
至此,一个页面多个倒计时就完成了, 记录学习。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
目录引言设计原则1. 明确不同角色的职责2. 发挥代码的威力,而不是限制3. 各个层面的可扩展性4. 专注而不是发散Sunmao 的工作原理响应最新的状态组件间交互布局与样式类型安全在组件间复用代码可扩展的可视化编辑器保持开放引言尽管现在越来越多的人开始对低代码开发感兴趣,但已有低代码方案的一些局限性仍然让大家有所保留
这篇文章主要介绍了Vue3 编写自定义指令插件的示例代码,主要包括编写自定义插件,在 main.ts 中加载启用插件的代码介绍,对Vue3自定义指令插件相关知识感兴趣的朋友一起看看吧
用JS怎样写一个简单计数器,方法是什么?一些朋友可能会遇到这方面的问题,对此在下文小编向大家来讲解一下,内容详细,易于理解,希望大家阅读完这篇能有收获哦,有需要的朋友就往下看吧!
vue3出来一段时间了,element也更新了版本去兼容vue3,下面这篇文章主要给大家介绍了关于vue3集成Element-plus实现按需自动引入组件的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考下
这篇文章主要为大家介绍了vue中filter的应用场景,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助<BR>
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 ICP核准(ICP备案)粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008