74bd629e27bb16b8e07ceecc8960083d2d0748c7..bf50dc43351d1a9781a84faf775913e813ea6ecb
4 天以前 Jacob
Merge branch 'dev' into sit
bf50dc 对比 | 目录
4 天以前 Jacob
fix: 专区倒计时改为数字拆分渲染并增加 token 防止多条定时器并发导致跳动
bfcd3b 对比 | 目录
1个文件已修改
86 ■■■■■ 已修改文件
pages/zone/index.vue 86 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
pages/zone/index.vue
....@@ -20,8 +20,9 @@
2020 <view v-else>
2121 <view class="expireTime" v-if="expireTime && expireTime.length">
2222 <text>专区将在</text>
23
- <text v-for="(val, index) in expireTime" :key="index">
24
- <text class="num">{{ val }}</text>{{ expireTimeUnit[index] }}
23
+ <text v-for="(item, index) in expireTime" :key="index">
24
+ <text class="num"><text v-for="(char, ci) in item.digits" :key="ci"
25
+ class="digit">{{ char }}</text></text>{{ item.unit }}
2526 </text>
2627 <text>{{ startOrEnd }}</text>
2728 </view>
....@@ -606,7 +607,7 @@
606607 showMenu: false,
607608 selectedMenuArr: [],
608609 expireTime: [],
609
- expireTimeUnit: [],
610
+ countdownToken: 0,
610611 isPageShow: false,
611612 loginBackFn: null,
612613 errText: '',
....@@ -1326,6 +1327,9 @@
13261327 })
13271328 },
13281329 validAlert(data) {
1330
+ // 每次重新计算前作废旧的倒计时链,避免多条定时器同时刷新导致数字跳动
1331
+ this.countdownToken++
1332
+ const token = this.countdownToken
13291333 const {
13301334 startTime,
13311335 expireTime
....@@ -1344,17 +1348,17 @@
13441348 let minutes = startTimetamp.getMinutes()
13451349 minutes = minutes < 10 ? '0' + minutes : minutes;
13461350
1347
- this.countdownTimer(startTime.split('-').join('/'), 'start')
1351
+ this.countdownTimer(startTime.split('-').join('/'), 'start', token)
13481352 this.startOrEnd = "后开启"
13491353 return
1350
- }
1351
- }
1352
- if (expireTime) {
1353
- const expireTimetamp = new Date(expireTime.split('-').join('/')).getTime();
1354
- if (expireTimetamp < nowTimestamp) {
1354
+ }
1355
+ }
1356
+ if (expireTime) {
1357
+ const expireTimetamp = new Date(expireTime.split('-').join('/')).getTime();
1358
+ if (expireTimetamp < nowTimestamp) {
13551359 return
1356
- }
1357
- this.countdownTimer(expireTime.split('-').join('/'), 'end')
1360
+ }
1361
+ this.countdownTimer(expireTime.split('-').join('/'), 'end', token)
13581362 this.startOrEnd = "后结束"
13591363 }
13601364 },
....@@ -1493,7 +1497,7 @@
14931497 that.homeTop = that.getHomeTop(0.4)
14941498 }
14951499 that.expireTime = [];
1496
- that.expireTimeUnit = [];
1500
+ that.countdownToken++;
14971501 that.where.page = 1;
14981502 that.getGoodList()
14991503 that.validAlert(res.data)
....@@ -1519,8 +1523,9 @@
15191523 }
15201524 });
15211525 },
1522
- countdownTimer(expireTime, type) {
1526
+ countdownTimer(expireTime, type, token) {
15231527 if (!expireTime) return
1528
+ if (token !== undefined && token !== this.countdownToken) return
15241529 let that = this;
15251530 // 结束时间的时间戳(需传入一个有效的日期对象或时间戳)
15261531 const endTimestamp = new Date(expireTime).getTime();
....@@ -1532,56 +1537,56 @@
15321537 if (type == 'end') {
15331538 if (diff <= 0) {
15341539 this.expireTime = []
1535
- this.expireTimeUnit = []
15361540 return
15371541 } else { }
15381542 } else if (type == 'start') {
15391543 if (diff <= 0) {
15401544 this.expireTime = []
1541
- this.expireTimeUnit = []
15421545 const expireTime2 = that.info.expireTime
15431546 if (expireTime2) {
15441547 const expireTimetamp = new Date(expireTime2.split('-').join('/')).getTime();
15451548 if (expireTimetamp < nowTimestamp) {
15461549 return
15471550 }
1548
- this.countdownTimer(expireTime2.split('-').join('/'), 'end')
1551
+ this.countdownTimer(expireTime2.split('-').join('/'), 'end', token)
15491552 this.startOrEnd = "后结束"
15501553 }
15511554 return
15521555 }
15531556 }
1557
+ const pad = num => (num < 10 ? '0' + num : String(num))
15541558 const res = []
1555
- const unit = []
15561559 const days = Math.floor(diff / (1000 * 60 * 60 * 24))
15571560 diff %= (1000 * 60 * 60 * 24)
1558
- if (days > 0) {
1559
- res.push(days);
1560
- unit.push('日')
1561
- }
15621561 // 转换为小时、分钟、秒
15631562 const hours = Math.floor(diff / (1000 * 60 * 60));
15641563 diff %= (1000 * 60 * 60); // 剩余毫秒数
1565
- if (hours > 0) {
1566
- res.push(hours < 10 ? '0' + hours : hours);
1567
- unit.push('时')
1568
- }
15691564 const minutes = Math.floor(diff / (1000 * 60));
1570
- if (minutes > 0) {
1571
- res.push(minutes < 10 ? '0' + minutes : minutes);
1572
- unit.push('分')
1573
- }
15741565 diff %= (1000 * 60); // 剩余毫秒数
15751566 const seconds = Math.floor(diff / 1000);
1576
- if (seconds > 0) {
1577
- res.push(seconds < 10 ? '0' + seconds : seconds);
1578
- unit.push('秒')
1567
+ // 天数为 0 时不展示,时/分/秒始终展示(补 0),保证倒计时长度稳定
1568
+ if (days > 0) {
1569
+ res.push({
1570
+ digits: pad(days).split(''),
1571
+ unit: '日'
1572
+ })
15791573 }
1574
+ res.push({
1575
+ digits: pad(hours).split(''),
1576
+ unit: '时'
1577
+ })
1578
+ res.push({
1579
+ digits: pad(minutes).split(''),
1580
+ unit: '分'
1581
+ })
1582
+ res.push({
1583
+ digits: pad(seconds).split(''),
1584
+ unit: '秒'
1585
+ })
15801586 this.expireTime = res
1581
- this.expireTimeUnit = unit
15821587 if (this.isPageShow) {
15831588 setTimeout(() => {
1584
- that.countdownTimer(expireTime, type)
1589
+ that.countdownTimer(expireTime, type, token)
15851590 }, 1000)
15861591 }
15871592 },
....@@ -2156,11 +2161,22 @@
21562161 padding: 15rpx;
21572162 padding-left: 30rpx;
21582163 color: #979797;
2164
+ font-size: 26rpx;
21592165
21602166 .num {
2167
+ display: inline-block;
21612168 color: #000000;
21622169 background: #FFE20E;
2163
- padding: 0 10rpx;
2170
+ padding: 0 6rpx;
2171
+ margin: 0 4rpx;
2172
+ vertical-align: middle;
2173
+ }
2174
+
2175
+ /* 每个数字固定宽度,避免倒计时跳动时宽度忽长忽短 */
2176
+ .digit {
2177
+ display: inline-block;
2178
+ width: 19rpx;
2179
+ text-align: center;
21642180 }
21652181 }
21662182 </style>