p***@163.com
p***@163.com
  • 发布:2019-07-31 20:50
  • 更新:2019-08-01 07:21
  • 阅读:1356

【报Bug】App端实现debounce出现错误

分类:uni-app

详细问题描述

(DCloud产品不会有明显的bug,所以你遇到的问题大都是在特定环境下才能重现的问题,请仔细描述你的环境和重现方式,否则DCloud很难排查解决你的问题)

[内容]
对scroll-view中的图片实现懒加载,需要在scroll事件中重新计算图片位置。
由于scroll-view滚动过快,希望使用debounce降低scroll事件中的代码调用频率。
测试发现h5端正常,但是在app端导致scroll要等timeout之后才响应

[步骤]
const debounce = function(fn, delay) {
var timer
return function() {
var context = this
var args = arguments
clearTimeout(timer)
timer = setTimeout(function() {
fn.apply(context, args)
}, delay)
}
};

export default {
debounce,
}

在onload中初始化
this.debounced_refresh_pic = debounce.debounce(this.refresh_pic, 1000);
延时1000ms

在scroll事件中调用
this.debounced_refresh_pic();

[结果]

h5端控制台打印结果

seller.vue:167 Thu Aug 01 2019 00:42:52 GMT+1200 (新西兰标准时间)
seller.vue:166 22
seller.vue:167 Thu Aug 01 2019 00:42:52 GMT+1200 (新西兰标准时间)
seller.vue:166 38
seller.vue:167 Thu Aug 01 2019 00:42:52 GMT+1200 (新西兰标准时间)
seller.vue:166 74
seller.vue:167 Thu Aug 01 2019 00:42:52 GMT+1200 (新西兰标准时间)
seller.vue:166 96
seller.vue:167 Thu Aug 01 2019 00:42:52 GMT+1200 (新西兰标准时间)
seller.vue:166 100
seller.vue:167 Thu Aug 01 2019 00:42:52 GMT+1200 (新西兰标准时间)
seller.vue:166 102
seller.vue:167 Thu Aug 01 2019 00:42:52 GMT+1200 (新西兰标准时间)
seller.vue:166 108
seller.vue:167 Thu Aug 01 2019 00:42:52 GMT+1200 (新西兰标准时间)
seller.vue:166 119
seller.vue:167 Thu Aug 01 2019 00:42:52 GMT+1200 (新西兰标准时间)
seller.vue:166 151
seller.vue:167 Thu Aug 01 2019 00:42:52 GMT+1200 (新西兰标准时间)
seller.vue:166 183
seller.vue:167 Thu Aug 01 2019 00:42:52 GMT+1200 (新西兰标准时间)
seller.vue:166 193
seller.vue:167 Thu Aug 01 2019 00:42:52 GMT+1200 (新西兰标准时间)
seller.vue:166 200
seller.vue:167 Thu Aug 01 2019 00:42:52 GMT+1200 (新西兰标准时间)
seller.vue:166 202
seller.vue:167 Thu Aug 01 2019 00:42:52 GMT+1200 (新西兰标准时间)
seller.vue:166 219
seller.vue:167 Thu Aug 01 2019 00:42:52 GMT+1200 (新西兰标准时间)
seller.vue:166 233
seller.vue:167 Thu Aug 01 2019 00:42:52 GMT+1200 (新西兰标准时间)
seller.vue:166 250
seller.vue:167 Thu Aug 01 2019 00:42:52 GMT+1200 (新西兰标准时间)
seller.vue:166 282
seller.vue:167 Thu Aug 01 2019 00:42:53 GMT+1200 (新西兰标准时间)
seller.vue:166 292
seller.vue:167 Thu Aug 01 2019 00:42:53 GMT+1200 (新西兰标准时间)
seller.vue:166 300
seller.vue:167 Thu Aug 01 2019 00:42:53 GMT+1200 (新西兰标准时间)
seller.vue:192 Thu Aug 01 2019 00:42:54 GMT+1200 (新西兰标准时间)
seller.vue:193
seller.vue:194 1564576974097
seller.vue:196 refresh

可见scroll频率不变,能够实现最终延时后的debounce

app控制台打印结果:

00:46:33.036 加载图片 at pages\seller\seller.vue:176
00:46:38.556 [Number] 0.3333333432674408 at pages\seller\seller.vue:166
00:46:38.576 Thu Aug 01 2019 00:46:38 GMT+1200 (NZST) at pages\seller\seller.vue:167
00:46:39.238 [Number] 7.666666507720947 at pages\seller\seller.vue:166
00:46:39.258 Thu Aug 01 2019 00:46:38 GMT+1200 (NZST) at pages\seller\seller.vue:167
00:46:39.762 [Number] 13 at pages\seller\seller.vue:166
00:46:39.782 Thu Aug 01 2019 00:46:39 GMT+1200 (NZST) at pages\seller\seller.vue:167
00:46:40.322 [Number] 18.33333396911621 at pages\seller\seller.vue:166
00:46:40.342 Thu Aug 01 2019 00:46:40 GMT+1200 (NZST) at pages\seller\seller.vue:167
00:46:40.822 [Number] 22.66666603088379 at pages\seller\seller.vue:166
00:46:40.842 Thu Aug 01 2019 00:46:40 GMT+1200 (NZST) at pages\seller\seller.vue:167
00:46:41.365 [Number] 26.66666603088379 at pages\seller\seller.vue:166
00:46:41.385 Thu Aug 01 2019 00:46:41 GMT+1200 (NZST) at pages\seller\seller.vue:167
00:46:41.932 [Number] 30.66666603088379 at pages\seller\seller.vue:166
00:46:41.952 Thu Aug 01 2019 00:46:41 GMT+1200 (NZST) at pages\seller\seller.vue:167
00:46:42.452 [Number] 35.33333206176758 at pages\seller\seller.vue:166
00:46:42.475 Thu Aug 01 2019 00:46:42 GMT+1200 (NZST) at pages\seller\seller.vue:167
00:46:42.995 [Number] 39.33333206176758 at pages\seller\seller.vue:166
00:46:43.015 Thu Aug 01 2019 00:46:42 GMT+1200 (NZST) at pages\seller\seller.vue:167
00:46:43.520 [Number] 42 at pages\seller\seller.vue:166
00:46:43.540 Thu Aug 01 2019 00:46:43 GMT+1200 (NZST) at pages\seller\seller.vue:167
00:46:44.088 [Number] 44.66666793823242 at pages\seller\seller.vue:166
00:46:44.107 Thu Aug 01 2019 00:46:43 GMT+1200 (NZST) at pages\seller\seller.vue:167
00:46:44.635 [Number] 47.33333206176758 at pages\seller\seller.vue:166
00:46:44.656 Thu Aug 01 2019 00:46:44 GMT+1200 (NZST) at pages\seller\seller.vue:167
00:46:45.159 [Number] 50 at pages\seller\seller.vue:166
00:46:45.179 Thu Aug 01 2019 00:46:44 GMT+1200 (NZST) at pages\seller\seller.vue:167
00:46:45.705 [Number] 52.66666793823242 at pages\seller\seller.vue:166
00:46:45.725 Thu Aug 01 2019 00:46:45 GMT+1200 (NZST) at pages\seller\seller.vue:167
00:46:46.245 [Number] 56 at pages\seller\seller.vue:166
00:46:46.265 Thu Aug 01 2019 00:46:45 GMT+1200 (NZST) at pages\seller\seller.vue:167
00:46:46.770 [Number] 59 at pages\seller\seller.vue:166
00:46:46.794 Thu Aug 01 2019 00:46:46 GMT+1200 (NZST) at pages\seller\seller.vue:167
00:46:47.312 [Number] 62.33333206176758 at pages\seller\seller.vue:166
00:46:47.332 Thu Aug 01 2019 00:46:47 GMT+1200 (NZST) at pages\seller\seller.vue:167
00:46:47.836 [Number] 65.66666412353516 at pages\seller\seller.vue:166
00:46:47.856 Thu Aug 01 2019 00:46:47 GMT+1200 (NZST) at pages\seller\seller.vue:167
00:46:48.382 [Number] 69.33333587646484 at pages\seller\seller.vue:166
00:46:48.402 Thu Aug 01 2019 00:46:48 GMT+1200 (NZST) at pages\seller\seller.vue:167
00:46:48.924 [Number] 72.66666412353516 at pages\seller\seller.vue:166
00:46:48.944 Thu Aug 01 2019 00:46:48 GMT+1200 (NZST) at pages\seller\seller.vue:167
00:46:49.449 [Number] 76 at pages\seller\seller.vue:166
00:46:49.469 Thu Aug 01 2019 00:46:49 GMT+1200 (NZST) at pages\seller\seller.vue:167
00:46:50.012 [Number] 79 at pages\seller\seller.vue:166
00:46:50.032 Thu Aug 01 2019 00:46:49 GMT+1200 (NZST) at pages\seller\seller.vue:167
00:46:50.516 [Number] 81.66666412353516 at pages\seller\seller.vue:166
00:46:50.536 Thu Aug 01 2019 00:46:50 GMT+1200 (NZST) at pages\seller\seller.vue:167
00:46:51.056 [Number] 84.66666412353516 at pages\seller\seller.vue:166
00:46:51.076 Thu Aug 01 2019 00:46:50 GMT+1200 (NZST) at pages\seller\seller.vue:167
00:46:51.627 [Number] 89.66666412353516 at pages\seller\seller.vue:166
00:46:51.647 Thu Aug 01 2019 00:46:51 GMT+1200 (NZST) at pages\seller\seller.vue:167
00:46:52.127 [Number] 122 at pages\seller\seller.vue:166
00:46:52.147 Thu Aug 01 2019 00:46:51 GMT+1200 (NZST) at pages\seller\seller.vue:167
00:46:52.692 [Number] 132 at pages\seller\seller.vue:166
00:46:52.712 Thu Aug 01 2019 00:46:52 GMT+1200 (NZST) at pages\seller\seller.vue:167
00:46:53.192 [Number] 147.3333282470703 at pages\seller\seller.vue:166
00:46:53.212 Thu Aug 01 2019 00:46:52 GMT+1200 (NZST) at pages\seller\seller.vue:167
00:46:53.863 [Number] 164.6666717529297 at pages\seller\seller.vue:166
00:46:53.886 Thu Aug 01 2019 00:46:53 GMT+1200 (NZST) at pages\seller\seller.vue:167
00:46:54.405 [Number] 180.6666717529297 at pages\seller\seller.vue:166
00:46:54.425 Thu Aug 01 2019 00:46:54 GMT+1200 (NZST) at pages\seller\seller.vue:167
00:46:54.925 [Number] 193.3333282470703 at pages\seller\seller.vue:166
00:46:54.945 Thu Aug 01 2019 00:46:54 GMT+1200 (NZST) at pages\seller\seller.vue:167
00:46:55.487 [Number] 202.3333282470703 at pages\seller\seller.vue:166
00:46:55.509 Thu Aug 01 2019 00:46:55 GMT+1200 (NZST) at pages\seller\seller.vue:167
00:46:56.030 [Number] 209 at pages\seller\seller.vue:166
00:46:56.050 Thu Aug 01 2019 00:46:55 GMT+1200 (NZST) at pages\seller\seller.vue:167
00:46:56.562 [Number] 214 at pages\seller\seller.vue:166
00:46:56.588 Thu Aug 01 2019 00:46:56 GMT+1200 (NZST) at pages\seller\seller.vue:167
00:46:57.084 [Number] 217.3333282470703 at pages\seller\seller.vue:166
00:46:57.105 Thu Aug 01 2019 00:46:56 GMT+1200 (NZST) at pages\seller\seller.vue:167
00:46:57.626 [Number] 219.6666717529297 at pages\seller\seller.vue:166
00:46:57.646 Thu Aug 01 2019 00:46:57 GMT+1200 (NZST) at pages\seller\seller.vue:167
00:46:58.168 [Number] 220.6666717529297 at pages\seller\seller.vue:166
00:46:58.190 Thu Aug 01 2019 00:46:57 GMT+1200 (NZST) at pages\seller\seller.vue:167
00:46:58.741 [Number] 245 at pages\seller\seller.vue:166
00:46:58.762 Thu Aug 01 2019 00:46:58 GMT+1200 (NZST) at pages\seller\seller.vue:167
00:46:59.264 [Number] 276.3333435058594 at pages\seller\seller.vue:166
00:46:59.284 Thu Aug 01 2019 00:46:58 GMT+1200 (NZST) at pages\seller\seller.vue:167
00:46:59.784 [Number] 316 at pages\seller\seller.vue:166
00:46:59.804 Thu Aug 01 2019 00:46:59 GMT+1200 (NZST) at pages\seller\seller.vue:167
00:47:00.370 [Number] 368.6666564941406 at pages\seller\seller.vue:166
00:47:00.390 Thu Aug 01 2019 00:47:00 GMT+1200 (NZST) at pages\seller\seller.vue:167
00:47:00.875 [Number] 411 at pages\seller\seller.vue:166
00:47:00.895 Thu Aug 01 2019 00:47:00 GMT+1200 (NZST) at pages\seller\seller.vue:167
00:47:01.415 [Number] 450.3333435058594 at pages\seller\seller.vue:166
00:47:01.435 Thu Aug 01 2019 00:47:01 GMT+1200 (NZST) at pages\seller\seller.vue:167
00:47:01.960 [Number] 485.3333435058594 at pages\seller\seller.vue:166
00:47:01.980 Thu Aug 01 2019 00:47:01 GMT+1200 (NZST) at pages\seller\seller.vue:167
00:47:02.480 [Number] 516 at pages\seller\seller.vue:166
00:47:02.501 Thu Aug 01 2019 00:47:02 GMT+1200 (NZST) at pages\seller\seller.vue:167
00:47:03.028 [Number] 543 at pages\seller\seller.vue:166
00:47:03.048 Thu Aug 01 2019 00:47:02 GMT+1200 (NZST) at pages\seller\seller.vue:167
00:47:03.542 [Number] 566.3333129882812 at pages\seller\seller.vue:166
00:47:03.561 Thu Aug 01 2019 00:47:03 GMT+1200 (NZST) at pages\seller\seller.vue:167
00:47:04.081 [Number] 577 at pages\seller\seller.vue:166
00:47:04.101 Thu Aug 01 2019 00:47:03 GMT+1200 (NZST) at pages\seller\seller.vue:167
00:47:04.633 [Number] 644.3333129882812 at pages\seller\seller.vue:166
00:47:04.653 Thu Aug 01 2019 00:47:04 GMT+1200 (NZST) at pages\seller\seller.vue:167
00:47:05.159 [Number] 654 at pages\seller\seller.vue:166
00:47:05.179 Thu Aug 01 2019 00:47:04 GMT+1200 (NZST) at pages\seller\seller.vue:167
00:47:05.701 [Number] 662.3333129882812 at pages\seller\seller.vue:166
00:47:05.723 Thu Aug 01 2019 00:47:05 GMT+1200 (NZST) at pages\seller\seller.vue:167
00:47:06.231 [Number] 669.6666870117188 at pages\seller\seller.vue:166
00:47:06.252 Thu Aug 01 2019 00:47:05 GMT+1200 (NZST) at pages\seller\seller.vue:167
00:47:06.771 [Number] 675.6666870117188 at pages\seller\seller.vue:166
00:47:06.791 Thu Aug 01 2019 00:47:06 GMT+1200 (NZST) at pages\seller\seller.vue:167
00:47:07.375 [Number] 680.6666870117188 at pages\seller\seller.vue:166
00:47:07.396 Thu Aug 01 2019 00:47:07 GMT+1200 (NZST) at pages\seller\seller.vue:167
00:47:07.916 [Number] 684.6666870117188 at pages\seller\seller.vue:166
00:47:07.939 Thu Aug 01 2019 00:47:07 GMT+1200 (NZST) at pages\seller\seller.vue:167
00:47:08.443 [Number] 688 at pages\seller\seller.vue:166
00:47:08.463 Thu Aug 01 2019 00:47:08 GMT+1200 (NZST) at pages\seller\seller.vue:167
00:47:09.005 [Number] 732 at pages\seller\seller.vue:166
00:47:09.025 Thu Aug 01 2019 00:47:08 GMT+1200 (NZST) at pages\seller\seller.vue:167
00:47:09.526 [Number] 778.6666870117188 at pages\seller\seller.vue:166
00:47:09.549 Thu Aug 01 2019 00:47:09 GMT+1200 (NZST) at pages\seller\seller.vue:167
00:47:10.072 [Number] 870.3333129882812 at pages\seller\seller.vue:166
00:47:10.092 Thu Aug 01 2019 00:47:09 GMT+1200 (NZST) at pages\seller\seller.vue:167
00:47:10.593 [Number] 959 at pages\seller\seller.vue:166
00:47:10.613 Thu Aug 01 2019 00:47:10 GMT+1200 (NZST) at pages\seller\seller.vue:167
00:47:11.135 [Number] 1043.3333740234375 at pages\seller\seller.vue:166
00:47:11.155 Thu Aug 01 2019 00:47:10 GMT+1200 (NZST) at pages\seller\seller.vue:167
00:47:11.666 [Number] 1411.3333740234375 at pages\seller\seller.vue:166
00:47:11.686 Thu Aug 01 2019 00:47:11 GMT+1200 (NZST) at pages\seller\seller.vue:167
00:47:12.187 [Number] 1462 at pages\seller\seller.vue:166
00:47:12.207 Thu Aug 01 2019 00:47:11 GMT+1200 (NZST) at pages\seller\seller.vue:167
00:47:12.729 [Number] 1508 at pages\seller\seller.vue:166
00:47:12.749 Thu Aug 01 2019 00:47:12 GMT+1200 (NZST) at pages\seller\seller.vue:167
00:47:13.247 [Number] 1550 at pages\seller\seller.vue:166
00:47:13.273 Thu Aug 01 2019 00:47:12 GMT+1200 (NZST) at pages\seller\seller.vue:167
00:47:13.780 [Number] 1588.6666259765625 at pages\seller\seller.vue:166
00:47:13.800 Thu Aug 01 2019 00:47:13 GMT+1200 (NZST) at pages\seller\seller.vue:167
00:47:14.320 [Number] 1624 at pages\seller\seller.vue:166
00:47:14.340 Thu Aug 01 2019 00:47:14 GMT+1200 (NZST) at pages\seller\seller.vue:167
00:47:14.864 [Number] 1656 at pages\seller\seller.vue:166
00:47:14.884 Thu Aug 01 2019 00:47:14 GMT+1200 (NZST) at pages\seller\seller.vue:167
00:47:15.657 [Number] 1685.6666259765625 at pages\seller\seller.vue:166
00:47:15.680 Thu Aug 01 2019 00:47:15 GMT+1200 (NZST) at pages\seller\seller.vue:167
00:47:16.181 [Number] 1712.3333740234375 at pages\seller\seller.vue:166
00:47:16.201 Thu Aug 01 2019 00:47:15 GMT+1200 (NZST) at pages\seller\seller.vue:167
00:47:16.701 [Number] 1737 at pages\seller\seller.vue:166
00:47:16.721 Thu Aug 01 2019 00:47:16 GMT+1200 (NZST) at pages\seller\seller.vue:167
00:47:17.245 [Number] 1759.3333740234375 at pages\seller\seller.vue:166
00:47:17.265 Thu Aug 01 2019 00:47:16 GMT+1200 (NZST) at pages\seller\seller.vue:167
00:47:17.786 [Number] 1769.6666259765625 at pages\seller\seller.vue:166
00:47:17.806 Thu Aug 01 2019 00:47:17 GMT+1200 (NZST) at pages\seller\seller.vue:167
00:47:18.326 [Number] 1849.3333740234375 at pages\seller\seller.vue:166
00:47:18.346 Thu Aug 01 2019 00:47:18 GMT+1200 (NZST) at pages\seller\seller.vue:167
00:47:18.886 [Number] 1860.6666259765625 at pages\seller\seller.vue:166
00:47:18.906 Thu Aug 01 2019 00:47:18 GMT+1200 (NZST) at pages\seller\seller.vue:167
00:47:19.389 [Number] 1870.6666259765625 at pages\seller\seller.vue:166
00:47:19.409 Thu Aug 01 2019 00:47:19 GMT+1200 (NZST) at pages\seller\seller.vue:167
00:47:19.991 [Number] 1875.3333740234375 at pages\seller\seller.vue:166
00:47:20.012 Thu Aug 01 2019 00:47:19 GMT+1200 (NZST) at pages\seller\seller.vue:167
00:47:20.521 [Number] 1883.6666259765625 at pages\seller\seller.vue:166
00:47:20.543 Thu Aug 01 2019 00:47:20 GMT+1200 (NZST) at pages\seller\seller.vue:167
00:47:21.065 [Number] 1891 at pages\seller\seller.vue:166
00:47:21.085 Thu Aug 01 2019 00:47:20 GMT+1200 (NZST) at pages\seller\seller.vue:167
00:47:21.626 [Number] 1897 at pages\seller\seller.vue:166
00:47:21.646 Thu Aug 01 2019 00:47:21 GMT+1200 (NZST) at pages\seller\seller.vue:167
00:47:22.148 [Number] 1902.3333740234375 at pages\seller\seller.vue:166
00:47:22.168 Thu Aug 01 2019 00:47:21 GMT+1200 (NZST) at pages\seller\seller.vue:167
00:47:22.691 [Number] 1906.6666259765625 at pages\seller\seller.vue:166
00:47:22.713 Thu Aug 01 2019 00:47:22 GMT+1200 (NZST) at pages\seller\seller.vue:167
00:47:23.226 [Number] 1910 at pages\seller\seller.vue:166
00:47:23.246 Thu Aug 01 2019 00:47:22 GMT+1200 (NZST) at pages\seller\seller.vue:167
00:47:23.866 [Number] 1912.6666259765625 at pages\seller\seller.vue:166
00:47:23.886 Thu Aug 01 2019 00:47:23 GMT+1200 (NZST) at pages\seller\seller.vue:167
00:47:24.406 [Number] 1914.3333740234375 at pages\seller\seller.vue:166
00:47:24.426 Thu Aug 01 2019 00:47:24 GMT+1200 (NZST) at pages\seller\seller.vue:167
00:47:24.949 [Number] 1915.3333740234375 at pages\seller\seller.vue:166
00:47:24.969 Thu Aug 01 2019 00:47:24 GMT+1200 (NZST) at pages\seller\seller.vue:167
00:47:25.522 App Hide at App.vue:45
00:47:25.549 App Show at App.vue:42
00:47:25.963 Thu Aug 01 2019 00:47:25 GMT+1200 (NZST) at pages\seller\seller.vue:192
00:47:25.983 at pages\seller\seller.vue:193
00:47:26.003 [Number] 1564577245649 at pages\seller\seller.vue:194
00:47:26.023 refresh at pages\seller\seller.vue:196

变成了每一次scroll都被延时1秒多, 整个过程app不响应其他操作

[期望]

[如果语言难以表述清晰,拍一个视频或截图,有图有真相]

IDE运行环境说明

[HBuilder 或 HBuilderX。如果你用其他工具开发uni-app,也需要在此说明]

[IDE版本号]

[windows版本号]

[mac版本号]

uni-app运行环境说明

[运行端是h5或app或某个小程序?]

[运行端版本号]

[项目是cli创建的还是HBuilderX创建的?如果是cli创建的,请更新到最新版cli再试]

[编译模式是老模板模式还是新的自定义组件模式?]

App运行环境说明

[Android版本号]

[iOS版本号]

[手机型号]

[模拟器型号]

附件

[IDE问题请提供HBuilderX运行日志。菜单帮助-查看运行日志,点右键打开文件所在目录,将log文件压缩成zip包上传]

[App问题请提供可重现问题的代码片段,你补充的细一点,问题就解决的快一点]

[App安装包或H5地址]

[可重现代码片段]

联系方式

[QQ]

2019-07-31 20:50 负责人:无 分享
已邀请:
p***@163.com

p***@163.com (作者)

看起来似乎是app端scroll-view的效率问题

p***@163.com

p***@163.com (作者)

基本山可以确定是scroll的效率导致的,scroll里面的子元素比较多,在scroll事件中监听了scroll的位置,导致scroll滚动受到影响。将位置监听语句一并放到debounce里面就好多了。

该问题目前已经被锁定, 无法添加新回复