Coding With Fun
Home Docker Django Node.js Articles FAQ

Anti-shake for vue


May 30, 2021 Article blog


Table of contents


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 requires stabilization, as follows;

<template>
  <div class="main">
      <el-input v-model="input" @change="changeSeletc" placeholder="请输入内容"></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 shown below

 Anti-shake for vue1