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

Vue+swiper realizes timeline effect

This article shares the specific code of vue+swip...

Detailed explanation of MySQL 30 military rules

1. Basic Specifications (1) InnoDB storage engine...

Limit input type (multiple methods)

1. Only Chinese characters can be input and pasted...

Research on the problem of flip navigation with tilted mouse

In this article, we will analyze the production of...

Example of how to deploy a Django project using Docker

It is also very simple to deploy Django projects ...

js to realize the production method of carousel

This article shares the specific code for js to r...

js to achieve a simple carousel effect

This article shares the specific code of js to ac...

A brief comparison of Props in React

Table of contents Props comparison of class compo...

Sample code for installing ASPNET.Core3.0 runtime in Linux

# The following examples are for x64-bit runtime ...

td width problem when td cells are merged

In the following example, when the width of the td...

Use semantic tags to write your HTML compatible with IE6,7,8

HTML5 adds more semantic tags, such as header, fo...

JavaScript implements product details of e-commerce platform

This article shares a common example of viewing p...

Usage and difference of Js module packaging exports require import

Table of contents 1. Commonjs exports and require...