Vue2.x - Example of using anti-shake and throttling

Vue2.x - Example of using anti-shake and throttling

utils:

// Anti-shake export const debounce = (func, wait = 3000, immediate = true) => {
 let timeout = null;
 return function() {
  let context = this;
  let args = arguments;
  if (timeout) clearTimeout(timeout);
  if (immediate) {
   var callNow = !timeout; //The first click is true, the second click within the time is false, and the first click is repeated when the time is up timeout = setTimeout(() => {
    timeout = null;
   }, wait); //Timer ID
   if (callNow) func.apply(context, args);
  } else {
   timeout = setTimeout(function() {
    func.apply(context, args);
   }, wait);
  }
 };
};
// Timestamp scheme export const throttleTime = (fn, wait = 2000) => {
 var pre = Date.now();
 return function() {
  var context = this;
  var args = arguments;
  var now = Date.now();
  if (now - pre >= wait) {
   fn.apply(context, args);
   pre = Date.now();
  }
 };
};
// Timer solution export const throttleSetTimeout = (fn, wait = 3000) => {
 var timer = null;
 return function() {
  var context = this;
  var args = arguments;
  if (!timer) {
   timer = setTimeout(function() {
    fn.apply(context, args);
    timer = null;
   }, wait);
  }
 };
};

Use in vue:

<template>
 <div class="main">
  <p>Anti-shake is executed immediately</p>
  <button @click="click1">Click</button>

  <br />

  <p>Anti-shake is not performed immediately</p>
  <button @click="click2">Click</button>

  <br />

  <p>Throttling Timestamp Scheme</p>
  <button @click="click3">Click</button>

  <br />

  <p>Throttling timer solution</p>
  <button @click="click4">Click</button>
 </div>
</template>

<script>
import { debounce, throttleTime, throttleSetTimeout } from './utils';
export default {
 methods: {
  click1: debounce(
   function() {
    console.log('Anti-shake is executed immediately');
   },
   2000,
   true
  ),
  click2: debounce(
   function() {
    console.log('Anti-shake is not executed immediately');
   },
   2000,
   false
  ),
  click3: throttleTime(function() {
   console.log('Throttling timestamp scheme');
  }),
  click4: throttleSetTimeout(function() {
   console.log('Throttling timer scheme');
  })
 },
};
</script>

<style scoped lang="scss">
* {
 margin: 0;
 font-size: 20px;
 user-select: none;
}
.main {
 position: absolute;
 left: 50%;
 transform: translateX(-50%);
 width: 500px;
}
button {
 margin-bottom: 100px;
}
</style>

explain:

Image stabilization:

Immediate execution version: If immediate is true, the command will be executed the first time it is clicked, and will not be executed if it is clicked again. When the wait time is over, the next click will take effect, that is, it will only be executed the first time.

principle:

When you click for the first time, timeoutID does not exist and callNow is true, so the target code is executed immediately. When you click for the second time, timeoutID exists and callNow is false, so the target code is not executed. When the wait time is over, timeoutID is set to null, and the immediate execution logic begins to repeat.

Non-immediate execution version: If immediate is false, the first click will not be executed. It will take effect after the wait time is over. That is, no matter how many times you click, only the last click event will be executed.

principle:

Use setTimeout to delay the execution of an event. If it is triggered multiple times, clearTimeout the last executed code and restart the timing. If no event is triggered during the timing, execute the target code.

Throttling:

Execute the target code at the wait frequency when the event is triggered continuously.

Effect:

The above is the details of the example of using Vue2.x-anti-shake and throttling. For more information about vue anti-shake and throttling, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • Analysis of examples of using anti-shake and throttling in Vue components
  • How to use anti-shake and throttling in Vue
  • Correct use of Vue function anti-shake and throttling
  • A brief analysis of VUE anti-shake and throttling
  • Using lodash in Vue to de-shake and throttle events
  • How to use anti-shake and throttling in Vue

<<:  Teach you a trick to achieve text comparison in Linux

>>:  mysql batch delete large amounts of data

Recommend

How to configure the Runner container in Docker

1. Create a runner container mk@mk-pc:~/Desktop$ ...

MySQL database import and export data error solution example explanation

Exporting Data Report an error SHOW VARIABLES LIK...

Summary of common functions of PostgreSQL regular expressions

Summary of common functions of PostgreSQL regular...

Detailed explanation of three commonly used web effects in JavaScript

Table of contents 1 element offset series 1.1 Off...

Detailed process of upgrading glibc dynamic library in centos 6.9

glibc is the libc library released by gnu, that i...

The difference between MySQL count(1), count(*), and count(field)

Table of contents 1. First look at COUNT 2. The d...

How to automatically backup mysql remotely under Linux

Preface: Basically, whether it is for our own use...

How to configure multiple projects with the same domain name in Nginx

There are two ways to configure multiple projects...

Summary of knowledge points about covering index in MySQL

If an index contains (or covers) the values ​​of ...

Use viewport in meta tag to define screen css

<meta name="viewport" content="w...

Server stress testing concepts and methods (TPS/concurrency)

Table of contents 1 Indicators in stress testing ...

The grid is your layout plan for the page

<br /> English original: http://desktoppub.a...