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

4 Ways to Quickly Teach Yourself Linux Commands

If you want to become a Linux master, then master...

Pure CSS to implement iOS style open and close selection box function

1 Effect Demo address: https://www.albertyy.com/2...

CSS uses radial-gradient to implement coupon styles

This article will introduce how to use radial-gra...

View MySQL installation information under Linux server

View the installation information of mysql: #ps -...

A simple method to regularly delete expired data records in MySQL

1. After connecting and logging in to MySQL, firs...

Docker's four network types principle examples

Four network types: None: Do not configure any ne...

select the best presets to create full compatibility with all browsersselect

We know that the properties of the select tag in e...

Detailed tutorial on using Docker to build Gitlab based on CentOS8 system

Table of contents 1. Install Docker 2. Install Gi...

A brief discussion on group by in MySQL

Table of contents 1. Introduction 2. Prepare the ...

Analysis of GTK treeview principle and usage

The GtkTreeView component is an advanced componen...

Example code for implementing equal height layout in multiple ways with CSS

The equal height layout described in this article...

In-depth understanding of the use of the infer keyword in typescript

Table of contents infer Case: Deepen your underst...

The table tbody in HTML can slide up and down and left and right

When the table header is fixed, it needs to be di...