Vue2.x-使用防抖以及節(jié)流的示例
utils:
// 防抖 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; //點(diǎn)擊第一次為true 時(shí)間內(nèi)點(diǎn)擊第二次為false 時(shí)間結(jié)束則重復(fù)第一次 timeout = setTimeout(() => { timeout = null; }, wait); //定時(shí)器ID if (callNow) func.apply(context, args); } else { timeout = setTimeout(function() { func.apply(context, args); }, wait); } }; }; // 時(shí)間戳方案 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(); } }; }; // 定時(shí)器方案 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); } }; };
vue中使用:
<template> <div class="main"> <p>防抖立即執(zhí)行</p> <button @click="click1">點(diǎn)擊</button> <br /> <p>防抖不立即執(zhí)行</p> <button @click="click2">點(diǎn)擊</button> <br /> <p>節(jié)流時(shí)間戳方案</p> <button @click="click3">點(diǎn)擊</button> <br /> <p>節(jié)流定時(shí)器方案</p> <button @click="click4">點(diǎn)擊</button> </div> </template> <script> import { debounce, throttleTime, throttleSetTimeout } from './utils'; export default { methods: { click1: debounce( function() { console.log('防抖立即執(zhí)行'); }, 2000, true ), click2: debounce( function() { console.log('防抖不立即執(zhí)行'); }, 2000, false ), click3: throttleTime(function() { console.log('節(jié)流時(shí)間戳方案'); }), click4: throttleSetTimeout(function() { console.log('節(jié)流定時(shí)器方案'); }) }, }; </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>
解釋?zhuān)?/h2>
防抖:
立即執(zhí)行版本:immediate為true,則點(diǎn)擊第一次就執(zhí)行,再繼續(xù)點(diǎn)擊則不執(zhí)行,當(dāng)wait時(shí)間結(jié)束后,再點(diǎn)擊則生效,也就是只執(zhí)行第一次。
原理:
點(diǎn)擊第一次不存在timeoutID,并且callNow為true,則立即執(zhí)行目標(biāo)代碼,點(diǎn)擊第二次時(shí)存在了timeoutID,并且callNow為false,所以不執(zhí)行目標(biāo)代碼,當(dāng)wait時(shí)間結(jié)束后,把timeoutID設(shè)為null,則開(kāi)始重復(fù)立即執(zhí)行邏輯。
不立即執(zhí)行版:immediate為false,則點(diǎn)擊第一次不執(zhí)行,當(dāng)wait時(shí)間結(jié)束后,才生效,也就是無(wú)論點(diǎn)擊多少次,只執(zhí)行最后一次點(diǎn)擊事件
原理:
使用setTimeout延遲執(zhí)行事件,如果多次觸發(fā),則clearTimeout上次執(zhí)行的代碼,重新開(kāi)始計(jì)時(shí),在計(jì)時(shí)期間沒(méi)有觸發(fā)事件,則執(zhí)行目標(biāo)代碼。
節(jié)流:
連續(xù)觸發(fā)事件時(shí)以wait頻率執(zhí)行目標(biāo)代碼。
效果:
以上就是Vue2.x-使用防抖以及節(jié)流的示例的詳細(xì)內(nèi)容,更多關(guān)于vue 防抖及節(jié)流的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
vue實(shí)現(xiàn)動(dòng)態(tài)路由的詳細(xì)代碼示例
動(dòng)態(tài)路由,動(dòng)態(tài)即不是寫(xiě)死的,是可變的,下面這篇文章主要給大家介紹了關(guān)于vue實(shí)現(xiàn)動(dòng)態(tài)路由的詳細(xì)代碼示例,文中通過(guò)圖文以及代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01vue-element-admin按鈕級(jí)權(quán)限管控的實(shí)現(xiàn)
開(kāi)發(fā)離不開(kāi)權(quán)限,不同的用戶登錄,根據(jù)不同的權(quán)限,可以訪問(wèn)不同的管理目錄,本文主要介紹了vue-element-admin按鈕級(jí)權(quán)限管控的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2022-04-04vue監(jiān)聽(tīng)scroll的坑的解決方法
這篇文章主要介紹了vue監(jiān)聽(tīng)scroll的坑的解決方法,現(xiàn)在分享給大家,也給大家做個(gè)參考,希望給有同樣經(jīng)歷的人幫助2017-09-09關(guān)于vue中對(duì)window.openner的使用指南
opener屬性是一個(gè)可讀可寫(xiě)的屬性,可返回對(duì)創(chuàng)建該窗口的Window對(duì)象的引用,下面這篇文章主要給大家介紹了關(guān)于vue中對(duì)window.openner使用的相關(guān)資料,需要的朋友可以參考下2022-11-11vue3.0報(bào)錯(cuò)Cannot?find?module‘worker_threads‘的解決辦法
這篇文章介紹了vue3.0報(bào)錯(cuò)Cannot?find?module‘worker_threads‘的解決辦法。對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-11-11