網站首頁 Vue 正文
使用防抖與節流,及this指向問題
最近項目中遇到了防抖與節流問題,搜索了很多文章都有this指向的問題,最后不得不采取一種很low的方法
data中定義isFirst為1
if (this.isFirst < 2){ ? ? this.isFirst = 2 ? ? setTimeout(() => { ? ? ? ? this.isFirst = 1 ? ? ? }, 1000) ? }
這樣就形成了假的節流
但是我們怎么能屈服于這種寫法
繼續探索vue項目中用閉包的方式防抖節流
一頓操作后
?const delay = (function () { ? ? let timeout ? ? return (callback, ms) => { ? ? ? if (timeout) clearTimeout(timeout) ? ? ? let callNow = !timeout ? ? ? timeout = setTimeout(() => { ? ? ? ? timeout = undefined ? ? ? }, ms) ? ? ? if (callNow) callback.apply(this, [callback, ms]) ? ? } ? })() ?export default { ?? ?methods: { ?? ??? ?delay(() => { ? ? ? ? ? // do something ? ? ? ?}, 1000) ?? ?} }
用了立即執行的函數方法,就能夠獲取到全局的this了?
使用防抖函數所遇見的坑
以前的防抖和節流都是在js中直接書寫,后使用vue進行組件化開發之后,有些地方需要注意。
正常寫法
? ? function debounce(func, delay) { ? ? ? let timeout ? ? ? return function () { ? ? ? ? let context = this; ? ? ? ? let args = arguments; ? ? ? ? if (timeout) { ? ? ? ? ? clearTimeout(timeout) ? ? ? ? } ? ? ? ? timeout = setTimeout(() => { ? ? ? ? ? func.apply(context, args) ? ? ? ? }, delay) ? ? ? } ? ? }
使用
? ? function change(volume, data) { ? ? ? debounce(() => { ? ? ? ? console.log('change', volume, data); ? ? ? }, 500) ? ? }
Vue中寫法使用
注意: Vue中使用時,需要定義timeout,同時在防抖函數中,this的指向發生了變化,需要在return之前獲取vue實例。這個時候,你直接使用,還是不行的,只要debug就會發現debounce返回的func沒有進去,需要手動執行一下(添加括號)。
? data() { ? ? return { ? ? ? timeout: null ? ? } ? }
? ? change(volume, data) { ? ? ? this.debounce(() => { ? ? ? ? console.log('change', volume, data) ? ? ? }, 500) ? ? }, ? ? debounce(func, delay) { ? ? ? let context = this // this指向發生變化,需要提出來 ? ? ? let args = arguments ? ? ? return function () { ? ? ? ? if (context.timeout) { ? ? ? ? ? clearTimeout(context.timeout) ? ? ? ? } ? ? ? ? context.timeout = setTimeout(() => { ? ? ? ? ? func.apply(context, args) ? ? ? ? }, delay) ? ? ? }()// 注意:我加了() ? ? }
Vue中的watch的防抖簡寫
? ? watchObj: { ? ? ? handler(val) { ? ? ? ? let _this = this ? ? ? ? clearTimeout(this.timeout) ? ? ? ? this.timeout = setTimeout(() => { ? ? ? ? ? _this.handlerData(val) ? ? ? ? }, 500) ? ? ? } ? ? }
原文鏈接:https://blog.csdn.net/DemonLhl/article/details/106074742
相關推薦
- 2023-07-03 Python?Thread虛假喚醒概念與防范詳解_python
- 2022-08-05 Entity?Framework主從表數據加載方式_C#教程
- 2022-03-19 基于React?Hooks的小型狀態管理詳解_React
- 2022-10-19 react封裝Dialog彈框的方法_React
- 2022-10-24 C語言進程程序替換的實現詳解_C 語言
- 2022-12-08 c++只保留float型的小數點后兩位問題_C 語言
- 2022-01-04 圖片網絡地址轉base64和文件
- 2022-02-21 React事件綁定詳解_React
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支