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:
|
<<: Teach you a trick to achieve text comparison in Linux
>>: mysql batch delete large amounts of data
1. Create a runner container mk@mk-pc:~/Desktop$ ...
Exporting Data Report an error SHOW VARIABLES LIK...
Summary of common functions of PostgreSQL regular...
Preface During the development process, we often ...
Table of contents 1 element offset series 1.1 Off...
glibc is the libc library released by gnu, that i...
Table of contents 1. First look at COUNT 2. The d...
Preface: Basically, whether it is for our own use...
There are two ways to configure multiple projects...
If an index contains (or covers) the values of ...
<meta name="viewport" content="w...
Table of contents 1 Indicators in stress testing ...
I have learned some basic selectors of CSS before...
<br /> English original: http://desktoppub.a...
Problem Description When using Windows Server 201...