当前位置:网站首页>Wechat applet: prevent multiple click jump (function throttling)
Wechat applet: prevent multiple click jump (function throttling)
2020-11-06 01:22:00 【:::::::】
scene
When using applets, there is a situation like this : When network conditions are poor or stuck , Users will think that the click is invalid and make multiple clicks , Many times the page jumps , As the figure below ( Two quick clicks ):
terms of settlement
And then from Easy to understand JS Function throttling and function buffeting We found a solution , Namely Function throttling (throttle): A function that fires multiple times over a period of time will only execute the first time , By the end of this period , No matter how many times it is triggered, it will not execute the function .
/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)
})
such , Crazy click on the button will only 1s Trigger once .
But then there's a problem , It's when you want to get this.data Got this yes undefined, Or want to get wechat components button Data passed to the click function e It's also undefined, therefore throttle Function also needs to do some processing to make it can be used in the wechat applet page js in .
The reason for this is throttle It returns a new function , It's not the original function anymore . The new function encapsulates the original function , So the component button The parameters passed are in the new function . So we need to pass these parameters to the functions that really need to be executed fn.
final throttle Function as follows :
function throttle(fn, gapTime) {
if (gapTime == null || gapTime == undefined) {
gapTime = 1500
}
let _lastTime = null
// Return the new function
return function () {
let _nowTime = + new Date()
if (_nowTime - _lastTime > gapTime || !_lastTime) {
fn.apply(this, arguments) // take this And parameters to the original function
_lastTime = _nowTime
}
}
}
Click the button again this and e Have it all. :
Reference resources
Source code
- tomfriwel/MyWechatAppDemo Of
throttlepage
Participation of this paper Tencent cloud media sharing plan , You are welcome to join us , share .
版权声明
本文为[:::::::]所创,转载请带上原文链接,感谢
边栏推荐
- DevOps是什么
- Relationship between business policies, business rules, business processes and business master data - modern analysis
- In depth understanding of the construction of Intelligent Recommendation System
- xmppmini 專案詳解:一步一步從原理跟我學實用 xmpp 技術開發 4.字串解碼祕笈與訊息包
- 教你轻松搞懂vue-codemirror的基本用法:主要实现代码编辑、验证提示、代码格式化
- IPFS/Filecoin合法性:保护个人隐私不被泄露
- The practice of the architecture of Internet public opinion system
- Python3 e-learning case 4: writing web proxy
- CCR炒币机器人:“比特币”数字货币的大佬,你不得不了解的知识
- htmlcss
猜你喜欢

From zero learning artificial intelligence, open the road of career planning!

一篇文章带你了解SVG 渐变知识

The difference between Es5 class and ES6 class

Thoughts on interview of Ali CCO project team

阿里云Q2营收破纪录背后,云的打开方式正在重塑

Windows 10 tensorflow (2) regression analysis of principles, deep learning framework (gradient descent method to solve regression parameters)

Didi elasticsearch cluster cross version upgrade and platform reconfiguration

CCR炒币机器人:“比特币”数字货币的大佬,你不得不了解的知识

如何玩转sortablejs-vuedraggable实现表单嵌套拖拽功能
![[JMeter] two ways to realize interface Association: regular representation extractor and JSON extractor](/img/cc/17b647d403c7a1c8deb581dcbbfc2f.jpg)
[JMeter] two ways to realize interface Association: regular representation extractor and JSON extractor
随机推荐
Working principle of gradient descent algorithm in machine learning
Serilog原始碼解析——使用方法
Skywalking series blog 2-skywalking using
Common algorithm interview has been out! Machine learning algorithm interview - KDnuggets
Real time data synchronization scheme based on Flink SQL CDC
熬夜总结了报表自动化、数据可视化和挖掘的要点,和你想的不一样
Word segmentation, naming subject recognition, part of speech and grammatical analysis in natural language processing
阿里云Q2营收破纪录背后,云的打开方式正在重塑
git rebase的時候捅婁子了,怎麼辦?線上等……
Elasticsearch 第六篇:聚合統計查詢
It's so embarrassing, fans broke ten thousand, used for a year!
What is the side effect free method? How to name it? - Mario
Filecoin最新动态 完成重大升级 已实现四大项目进展!
Linked blocking Queue Analysis of blocking queue
Do not understand UML class diagram? Take a look at this edition of rural love class diagram, a learn!
[JMeter] two ways to realize interface Association: regular representation extractor and JSON extractor
中小微企业选择共享办公室怎么样?
每个前端工程师都应该懂的前端性能优化总结:
Can't be asked again! Reentrantlock source code, drawing a look together!
多机器人行情共享解决方案