当前位置:网站首页>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 .
版权声明
本文为[:::::::]所创,转载请带上原文链接,感谢
边栏推荐
- Installing the consult cluster
- 基於MVC的RESTFul風格API實戰
- 前端基础牢记的一些操作-Github仓库管理
- Filecoin最新动态 完成重大升级 已实现四大项目进展!
- Python Jieba segmentation (stuttering segmentation), extracting words, loading words, modifying word frequency, defining thesaurus
- Python自动化测试学习哪些知识?
- 全球疫情加速互联网企业转型,区块链会是解药吗?
- git rebase的時候捅婁子了,怎麼辦?線上等……
- ES6学习笔记(五):轻松了解ES6的内置扩展对象
- Deep understanding of common methods of JS array
猜你喜欢

PHPSHE 短信插件说明

多机器人行情共享解决方案

IPFS/Filecoin合法性:保护个人隐私不被泄露

從小公司進入大廠,我都做對了哪些事?

Flink的DataSource三部曲之二:内置connector

Python自动化测试学习哪些知识?

Mongodb (from 0 to 1), 11 days mongodb primary to intermediate advanced secret

至联云分享:IPFS/Filecoin值不值得投资?

关于Kubernetes 与 OAM 构建统一、标准化的应用管理平台知识!(附网盘链接)

Aprelu: cross border application, adaptive relu | IEEE tie 2020 for machine fault detection
随机推荐
Elasticsearch 第六篇:聚合統計查詢
快快使用ModelArts,零基础小白也能玩转AI!
Filecoin的经济模型与未来价值是如何支撑FIL币价格破千的
Mongodb (from 0 to 1), 11 days mongodb primary to intermediate advanced secret
Can't be asked again! Reentrantlock source code, drawing a look together!
htmlcss
Just now, I popularized two unique skills of login to Xuemei
From zero learning artificial intelligence, open the road of career planning!
一篇文章带你了解CSS3图片边框
Natural language processing - wrong word recognition (based on Python) kenlm, pycorrector
6.6.1 localeresolver internationalization parser (1) (in-depth analysis of SSM and project practice)
[event center azure event hub] interpretation of error information found in event hub logs
在大规模 Kubernetes 集群上实现高 SLO 的方法
从海外进军中国,Rancher要执容器云市场牛耳 | 爱分析调研
Serilog原始碼解析——使用方法
Don't go! Here is a note: picture and text to explain AQS, let's have a look at the source code of AQS (long text)
Use of vuepress
Classical dynamic programming: complete knapsack problem
TRON智能钱包PHP开发包【零TRX归集】
How to select the evaluation index of classification model