PrefaceIn a movie project, I want to save the current position of the drop-down in the movie list to prevent you from switching pages and then switching back to the current movie list page, which would cause it to return to the first piece of movie data. At this time, I don’t want to save the current position every time I slide a little. I want to save it once every period of time. At this time, I can use anti-shake and throttling. conceptTo put it simply, anti-shake throttling is to use a timer to achieve our goal. Debounce: The callback is executed n seconds after the event is triggered. If it is triggered again within n seconds, the timing is restarted. A typical example is input box search: the search request is made n seconds after the input is completed, and the time is reset if more content is input within n seconds. Throttle: It is stipulated that a function can only be triggered once within a unit of time. If a function is triggered multiple times within this unit of time, only one will take effect. A typical case is that the mouse is continuously clicked to trigger, and it is stipulated that multiple clicks within n seconds will only take effect once. StabilizationdefinitionFrequent operations prevent jitter. If no operation is performed within n seconds after the operation, the event will be triggered. If the operation continues, the timing will be reset. Usage scenarios
Code// utils.js // immediate: whether to start executing function immediately debounce(func, delay = 300, immediate = false) { let timer = null return function() { if (timer) { clearTimeout(timer) } if (immediate && !timer) { func.apply(this, arguments) } timer = setTimeout(() => { func.apply(this, arguments) }, delay) } } Use in VueMethod 1: Write it in the public method utils and introduce it import { debounce } from 'utils' methods: { appSearch:debounce(function(e.target.value){ this.handleSearch(value) }, 1000), handleSearch(value) { console.log(value) } } Method 2: Write in the current vue file data: () => { return { debounceInput: () => {} } }, methods: { showApp(value) { console.log('value', value) }, debounce(func, delay = 300, immediate = false) { let timer = null return function() { if (timer) { clearTimeout(timer) } if (immediate && !timer) { func.apply(this, arguments) } timer = setTimeout(() => { func.apply(this, arguments) }, delay) } } }, mounted() { this.debounceInput = this.debounce(this.showApp, 1000) }, ThrottlingdefinitionFrequent operations dilute the execution of the function. When frequent operations occur, it is triggered only once every n seconds. Usage scenarios
Code// utils.js function throttle (func, delay = 300) { let prev = 0 return function() { let now = Date.now() if ((now - prev) >= delay) { func.apply(this, arguments) prev = Date.now() } } } Use in VueThe usage is the same as anti-shake SummarizeThis is the end of this article about the use of anti-shake and throttling in Vue. For more relevant content on the use of Vue anti-shake and throttling, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Use of Linux relative and absolute paths
>>: Specific use of Linux gcc command
Good database specifications help reduce the comp...
Well, you may be a design guru, or maybe that'...
1.v-bind (abbreviation:) To use data variables de...
Due to some of its own characteristics (locking t...
more is one of our most commonly used tools. The ...
Table of contents Overview Object rest attribute ...
This article shares the specific code of the appl...
mysql master-slave configuration 1. Preparation H...
This article shares the specific code of Bootstra...
Table of contents 1 Question 2 Methods 3 Experime...
Table of contents 1. Rename table method 2. Notes...
IE8 will have multiple compatibility modes . IE pl...
Table of contents JavaScript Objects 1. Definitio...
npm uninstall sudo npm uninstall npm -g If you en...
Virtualization 1. Environment Centos7.3 Disable s...