当前位置:网站首页>微信小程序:防止多次点击跳转(函数节流)
微信小程序:防止多次点击跳转(函数节流)
2020-11-06 01:22:00 【:::::::】
场景
在使用小程序的时候会出现这样一种情况:当网络条件差或卡顿的情况下,使用者会认为点击无效而进行多次点击,最后出现多次跳转页面的情况,就像下图(快速点击了两次):
解决办法
然后从 轻松理解JS函数节流和函数防抖 中找到了解决办法,就是函数节流(throttle):函数在一段时间内多次触发只会执行第一次,在这段时间结束前,不管触发多少次也不会执行函数。
/utils/util.js:
function throttle(fn, gapTime) { if (gapTime == null || gapTime == undefined) { gapTime = 1500 } let _lastTime = null return function () { let _nowTime = + new Date() if (_nowTime - _lastTime > gapTime || !_lastTime) { fn() _lastTime = _nowTime } } } module.exports = { throttle: throttle }
/pages/throttle/throttle.wxml:
<button bindtap='tap' data-key='abc'>tap</button>
/pages/throttle/throttle.js
const util = require('../../utils/util.js') Page({ data: { text: 'tomfriwel' }, onLoad: function (options) { }, tap: util.throttle(function (e) { console.log(this) console.log(e) console.log((new Date()).getSeconds()) }, 1000) })
这样,疯狂点击按钮也只会1s触发一次。
但是这样的话出现一个问题,就是当你想要获取this.data
得到的this
是undefined
, 或者想要获取微信组件button
传递给点击函数的数据e
也是undefined
,所以throttle
函数还需要做一点处理来使其能用在微信小程序的页面js
里。
出现这种情况的原因是throttle
返回的是一个新函数,已经不是最初的函数了。新函数包裹着原函数,所以组件button
传递的参数是在新函数里。所以我们需要把这些参数传递给真正需要执行的函数fn
。
最后的throttle
函数如下:
function throttle(fn, gapTime) { if (gapTime == null || gapTime == undefined) { gapTime = 1500 } let _lastTime = null // 返回新的函数 return function () { let _nowTime = + new Date() if (_nowTime - _lastTime > gapTime || !_lastTime) { fn.apply(this, arguments) //将this和参数传给原函数 _lastTime = _nowTime } } }
再次点击按钮this
和e
都有了:
参考
源代码
- tomfriwel/MyWechatAppDemo 的
throttle
页面
本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。
版权声明
本文为[:::::::]所创,转载请带上原文链接,感谢
https://cloud.tencent.com/developer/article/1715168
边栏推荐
- Grouping operation aligned with specified datum
- 100元扫货阿里云是怎样的体验?
- python 保存list数据
- 2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
- CCR炒币机器人:“比特币”数字货币的大佬,你不得不了解的知识
- 6.9.2 session flashmapmanager redirection management
- Flink的DataSource三部曲之二:内置connector
- (1) ASP.NET Introduction to core3.1 Ocelot
- Sort the array in ascending order according to the frequency
- 直播预告 | 微服务架构学习系列直播第三期
猜你喜欢
DRF JWT authentication module and self customization
你的财务报告该换个高级的套路了——财务分析驾驶舱
词嵌入教程
PLC模拟量输入和数字量输入是什么
业内首发车道级导航背后——详解高精定位技术演进与场景应用
Didi elasticsearch cluster cross version upgrade and platform reconfiguration
Flink的DataSource三部曲之二:内置connector
(1) ASP.NET Introduction to core3.1 Ocelot
全球疫情加速互联网企业转型,区块链会是解药吗?
Using Es5 to realize the class of ES6
随机推荐
hadoop 命令总结
Aprelu: cross border application, adaptive relu | IEEE tie 2020 for machine fault detection
從小公司進入大廠,我都做對了哪些事?
幽默:黑客式编程其实类似机器学习!
iptables基礎原理和使用簡介
GUI 引擎评价指标
Real time data synchronization scheme based on Flink SQL CDC
网络安全工程师演示:原来***是这样获取你的计算机管理员权限的!【维持】
Can't be asked again! Reentrantlock source code, drawing a look together!
Elasticsearch 第六篇:聚合統計查詢
用Python构建和可视化决策树
Asp.Net Core學習筆記:入門篇
安装Anaconda3 后,怎样使用 Python 2.7?
[performance optimization] Nani? Memory overflow again?! It's time to sum up the wave!!
“颜值经济”的野望:华熙生物净利率六连降,收购案遭上交所问询
[C#] (原創)一步一步教你自定義控制元件——04,ProgressBar(進度條)
分布式ID生成服务,真的有必要搞一个
微服務 - 如何解決鏈路追蹤問題
Examples of unconventional aggregation
前端模組化簡單總結