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

Multiple ways to insert SVG into HTML pages

SVG (Scalable Vector Graphics) is an image format...

mysql 5.6.23 winx64.zip installation detailed tutorial

For detailed documentation on installing the comp...

Use VSCode's Remote-SSH to connect to Linux for remote development

Install Remote-SSH and configure it First open yo...

A simple method to modify the size of Nginx uploaded files

Original link: https://vien.tech/article/138 Pref...

Docker configures the storage location of local images and containers

Use the find command to find files larger than a ...

Share 8 MySQL pitfalls that you have to mention

MySQL is easy to install, fast and has rich funct...

Comparing the performance of int, char, and varchar in MySQL

There are many seemingly true "rumors" ...

...

Vue implements the frame rate playback of the carousel

This article example shares the specific code of ...

How to use jsonp in vue

Table of contents 1. Introduction 2. Installation...

How to connect XShell and network configuration in CentOS7

1. Linux network configuration Before configuring...

VMware virtual machine to establish HTTP service steps analysis

1. Use xshell to connect to the virtual machine, ...

uni-app implements NFC reading function

This article shares the specific code of uni-app ...