How to prevent multiple click jumps caused by WeChat mini program function throttling

小云云
Release: 2018-01-27 10:37:59
Original
3404 people have browsed it

When studying this article, we need to know what function throttling is,The meaning of function throttling and anti-shake, and then this article will I mainly want to share with you how to prevent multiple click jumps due to the WeChat applet function throttling. I hope it will be useful to everyone.

Scenario

When using the mini program, there will be a situation like this: when the network conditions are poor or stuck In the case of pauses, users will think that the click is invalid and click multiple times, and finally jump to the page multiple times.

Solution

Then I found the solution from Easily understand JS function throttling and function anti-shake, It is function throttling: if a function is triggered multiple times within a period of time, it will only be executed for the first time. Before the end of this period, the function will not be executed no matter how many times it is triggered.

/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:

/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)
})
Copy after login

In this way, crazy clicking on the button will only trigger once per second.

But there is a problem in this case, that is, when you want to get this.data, the this you get is undefined, or if you want to get the data e passed to the click function by the WeChat component button, it is also undefined. , so the throttle function still needs some processing to make it available in the page js of the WeChat applet.

How to prevent multiple click jumps caused by WeChat mini program function throttling

The reason for this situation is that throttle returns a new function, which is no longer the original function. The new function wraps the original function, so the parameters passed by the component button are in the new function. So we need to pass these parameters to the function fn that actually needs to be executed.

The final throttle function is as follows:

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
}
}
}
Copy after login

Click the button again and both this and e are available:

How to prevent multiple click jumps caused by WeChat mini program function throttling

Related recommendations:

How to implement a session memo applet

Detailed explanation of JavaScript functions Throttle

Detailed explanation of JavaScript function throttling concepts and usage examples

The above is the detailed content of How to prevent multiple click jumps caused by WeChat mini program function throttling. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!