当前位置:网站首页>微信小程序实现活动倒计时
微信小程序实现活动倒计时
2022-08-04 05:38:00 【苏苏就是小苏苏】
1.实现效果
2.实现步骤
1.获取指定截止时间与当前时间的毫秒,即为剩余毫秒值,若剩余毫秒值小于0则返回0。
2.格式化当前毫秒,自动补0,转换成一个数组,格式为[‘天’,‘时’, ‘分’, ‘秒’]。
eg:
[364,11,30,07]
3.可进一步将[‘天’,‘时’, ‘分’, ‘秒’]拆分成[[,.],‘[,]’, ‘[,]’, ‘[,]’]。
eg:
[[3,6,4],[1,1],[3,0],[0,7]]
3.实现代码
<view class="flex-row time-box j_c">
<view class="time-item">{
{
left_time_list[0]}}</view>天
<view class="time-item">{
{
left_time_list[1]}}</view>时
<view class="time-item">{
{
left_time_list[2]}}</view>分
<view class="time-item">{
{
left_time_list[3]}}</view>秒
</view>
<view class="flex-row time-box j_c ">
<view class="flex-row">
<block wx:for="{
{left_time_list_date[0]}}" wx:key="list">
<view class="time-item1">{
{
item}}</view>
</block>
<text>天</text>
</view>
<view class="flex-row">
<block wx:for="{
{left_time_list_date[1]}}" wx:key="list">
<view class="time-item1">{
{
item}}</view>
</block>
<text>时</text>
</view>
<view class="flex-row">
<block wx:for="{
{left_time_list_date[2]}}" wx:key="list">
<view class="time-item1">{
{
item}}</view>
</block>
<text>分</text>
</view>
<view class="flex-row">
<block wx:for="{
{left_time_list_date[3]}}" wx:key="list">
<view class="time-item1">{
{
item}}</view>
</block>
<text>秒</text>
</view>
</view>
/* pages/actualPage/countdown/index.wxss */
.time-box {
font-size: 26rpx;
color: #EAA81B;
margin-bottom: 40rpx;
border-radius: 20rpx;
padding: 20px;
background: #fff;
margin: 20rpx;
}
.time-item {
width: 60rpx;
height: 40rpx;
background: linear-gradient(90deg, #FFEBB1 0%, #FFDB8F 100%);
border-radius: 4rpx;
font-size: 24rpx;
text-align: center;
line-height: 40rpx;
margin: 0 20rpx;
}
.time-item1 {
width: 30rpx;
height: 40rpx;
background: linear-gradient(90deg, #FFEBB1 0%, #FFDB8F 100%);
border-radius: 4rpx;
font-size: 24rpx;
text-align: center;
line-height: 40rpx;
margin: 0 10rpx;
}
Page({
data: {
left_time: '', //活动剩余时间的秒数
timer: '', //倒计时定时器
left_time_list: [], //剩余秒数转换 天,小时 分 秒
left_time_list_date: []
},
onShow: function () {
this.getLeftTime('2023/08/03 11:20:00')
},
/** * 定时器,计算剩下时间 * @param {*} end_time */
getLeftTime(end_time) {
let left_time = this.getTimestap(end_time);
this.initDate(left_time)
this.data.timer = setInterval(() => {
if (left_time-- === 0) {
this.setData({
left_time: 0,
left_time_list: this.formatSeconds(0)
})
clearInterval(this.data.timer)
} else {
this.initDate(left_time)
}
}, 1000);
},
/** * 初始化数据 */
initDate(e) {
let left_time_list = this.formatSeconds(e),
left_time_list_date = this.formatDate(JSON.stringify(left_time_list));
this.setData({
left_time: e,
left_time_list,
left_time_list_date
})
},
/** * 获取指定时间-当前时间的秒数 * @param {*} end_time */
getTimestap(end_time) {
// 当前时间
var currentTime = parseInt(new Date().getTime() / 1000);
// 未来时间
var futureTime = parseInt(new Date(end_time.replace(/-/g, '/')).getTime() / 1000); //ios无法解析
return futureTime <= currentTime ? 0 : futureTime - currentTime;
},
/** * 毫秒-天-时-分-秒 * @param {*} value */
formatSeconds(value) {
let time = [],
day = parseInt(value / 86400),
hour = parseInt((value % 86400) / 3600),
min = parseInt(((value % 86400) % 3600) / 60),
sec = parseInt(((value % 86400) % 3600) % 60);
time[0] = day > 0 ? this.addZero(day) : this.addZero(0);
time[1] = hour > 0 ? this.addZero(hour) : this.addZero(0);
time[2] = min > 0 ? this.addZero(min) : this.addZero(0);
time[3] = sec > 0 ? this.addZero(sec) : this.addZero(0);
return time;
},
/** * 拆分数组-天-时-分-秒 * @param {*} e */
formatDate(e) {
let list = JSON.parse(e);
for (let i = 0; i < list.length; i++) {
list[i] = list[i].toString().split('');
}
return list;
},
/** * 补0 * @param {*} num */
addZero(num) {
return num < 10 ? "0" + num : num;
},
onUnload() {
if (this.data.timer) {
console.log('销毁计时器')
clearInterval(this.data.timer)
}
}
})
4.更多小程序相关,关注公众号 苏苏的bug,更多小程序demo,尽在苏苏的码云如果对你有帮助,欢迎你的star+订阅!
边栏推荐
- Mac安装PHP开发环境
- Online public account article content to audio file practical gadget
- Provide 和 Inject 的用法
- 核心价值观编码器【matlab版】
- IDEA 控制台 中文乱码问题(如果网上教程都无法解决你的问题的话)
- 字符串的一些方法
- ResNet详解:ResNet到底在解决什么问题?
- Based on the EEMD + + MLR GRU helped time series prediction
- VMD结合ISSA优化LSSVM功率预测
- A priori box (Anchor) in target detection
猜你喜欢
Database document generation tool V1.0
Gramm Angle field GAF time-series data into the image and applied to the fault diagnosis
窥探晶体世界的奥秘 —— 230种空间群晶体结构模型全在这里
ThreadLocal内存泄漏问题讲解
MySQL(4)
解决腾讯云DescribeInstances api查询20条记录以上的问题
误差指标分析计算之matlab实现【开源1.0.0版】
用手机也能轻松玩转MATLAB编程
目标检测中的IoU、GIoU、DIoU与CIoU
格拉姆角场GAF将时序数据转换为图像并应用于故障诊断
随机推荐
golang 的库引用方法
Software: Recommend a domestic and very easy-to-use efficiency software uTools to everyone
golang chan
Time Series Forecasting Based on Reptile Search RSA Optimized LSTM
对产品设计,架构设计的一点思考
ES6新语法:symbol,map容器
Computer software: recommend a disk space analysis tool - WizTree
Database knowledge: SQLServer creates non-sa user notes
网络端口大全
七夕专属程序员的浪漫
ffmpeg打开rtsp流应该设置的几个参数
Different lower_case_table_names settings for server (‘1‘) and data dictionary (‘0‘) 解决方案
GRNN、RBF、PNN、KELM之间究竟有什么联系?
电脑知识:台式电脑应该选择品牌和组装,值得收藏
IoU, GIoU, DIoU and CIoU in target detection
DropBlock: 卷积层的正则化方法及复现代码
Microsoft Store 微软应用商店无法连接网络,错误代码:0x80131500
VMD combined with ISSA to optimize LSSVM power prediction
软件:给大家推荐一款国产非常好用的效率软件uTools
解决腾讯云DescribeInstances api查询20条记录以上的问题