Example code for implementing anti-shake in Vue

Example code for implementing anti-shake in Vue

Anti-shake: Prevent repeated clicks from triggering events

First of all, what is shaking? Shaking is a shiver! Originally I clicked once, now I clicked 3 times! I wonder if my friend has a good sense of picture in his mind! Hahahahahaha

A typical application is to prevent users from repeatedly clicking to request data.

Vue implements anti-shake method as follows:

1. First, create a new debounce.js code as follows

const debounce = function (fn, delay) {
	let timer = null
	return function(){
		let content = this;
		let args = arguments;
		if(timer){
			clearTimeout(timer)
		}
		timer = setTimeout(()=>{
			fn.apply(content,args)
		}, delay)
	}
}
export default debounce

2. Introduce debounce in the vue file that needs anti-shake, the content is as follows; this is a 500ms anti-shake of an input box

<template>
 <div class="main">
  <el-input v-model="input" @change="changeSeletc" placeholder="Please enter content"></el-input>
 </div>
</template>
<script>
 import debounce from "../utils/debounce"
 export default {
  name: "alarm",
  data(){
   return {
    input: ''
   }
  },
  methods:{
   changeSeletc:debounce(function () {
    console.log(this.input)
   },500),
  }
 }
</script>
<style scoped>
</style>

3. The effect is as shown below

Summarize

This is the end of this article about Vue's anti-shake implementation. For more relevant Vue anti-shake content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • A brief discussion on the best solution for VUE anti-shake and throttling (functional components)
  • Vue's latest anti-shake solution (must read)
  • Detailed explanation of Vue's click event anti-shake and throttling processing
  • Vue implements mobile phone number verification example code (application scenario of anti-shake function)
  • How to implement anti-shake operation when there are multiple input search boxes on a Vue.js page
  • Implement lodash's debounce anti-shake in the vue+element ui framework
  • A brief analysis of the use of Vue anti-shake and throttling
  • Example code for preventing multiple triggering and terminating multiple requests in Axios in Vue (anti-shake)
  • Use anti-shake and throttling in vue to prevent repeated clicks or repeated pull-up loading instances
  • Understanding and application of function anti-shake throttling in Vue

<<:  Detailed explanation of uniapp's global variable implementation

>>:  Detailed explanation of the usage of Object.assign() in ES6

Recommend

How to shrink the log file in MYSQL SERVER

The transaction log records the operations on the...

How to configure Basic Auth login authentication in Nginx

Sometimes we build a file server through nginx, w...

Introduction and analysis of three Binlog formats in MySQL

one. Mysql Binlog format introduction Mysql binlo...

Detailed use of Echarts in vue2 vue3

Table of contents 1. Installation 2. Use Echarts ...

Installation and use of Apache stress testing tools

1. Download Go to the Apache official website htt...

CentOS7 upgrade kernel kernel5.0 version

Upgrade process: Original system: CentOS7.3 [root...

Build a severe weather real-time warning system with Node.JS

Table of contents Preface: Step 1: Find the free ...

Vue implementation counter case

This article example shares the specific code of ...

React+Amap obtains latitude and longitude in real time and locates the address

Table of contents 1. Initialize the map 2. Map Po...

HTML optimization techniques you must know

To improve the performance of web pages, many dev...

WeChat applet implements text scrolling

This article example shares the specific code for...