在Vue中使用防抖與節(jié)流,及this指向的問(wèn)題
使用防抖與節(jié)流,及this指向問(wèn)題
最近項(xiàng)目中遇到了防抖與節(jié)流問(wèn)題,搜索了很多文章都有this指向的問(wèn)題,最后不得不采取一種很low的方法
data中定義isFirst為1
if (this.isFirst < 2){
? ? this.isFirst = 2
? ? setTimeout(() => {
? ? ? ? this.isFirst = 1
? ? ? }, 1000)
? }這樣就形成了假的節(jié)流
但是我們?cè)趺茨芮谶@種寫(xiě)法
繼續(xù)探索vue項(xiàng)目中用閉包的方式防抖節(jié)流
一頓操作后
?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)
?? ?}
}用了立即執(zhí)行的函數(shù)方法,就能夠獲取到全局的this了
使用防抖函數(shù)所遇見(jiàn)的坑
以前的防抖和節(jié)流都是在js中直接書(shū)寫(xiě),后使用vue進(jìn)行組件化開(kāi)發(fā)之后,有些地方需要注意。
正常寫(xiě)法
? ? 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中寫(xiě)法使用
注意: Vue中使用時(shí),需要定義timeout,同時(shí)在防抖函數(shù)中,this的指向發(fā)生了變化,需要在return之前獲取vue實(shí)例。這個(gè)時(shí)候,你直接使用,還是不行的,只要debug就會(huì)發(fā)現(xiàn)debounce返回的func沒(méi)有進(jìn)去,需要手動(dòng)執(zhí)行一下(添加括號(hào))。
? data() {
? ? return {
? ? ? timeout: null
? ? }
? }? ? change(volume, data) {
? ? ? this.debounce(() => {
? ? ? ? console.log('change', volume, data)
? ? ? }, 500)
? ? },
? ? debounce(func, delay) {
? ? ? let context = this // this指向發(fā)生變化,需要提出來(lái)
? ? ? let args = arguments
? ? ? return function () {
? ? ? ? if (context.timeout) {
? ? ? ? ? clearTimeout(context.timeout)
? ? ? ? }
? ? ? ? context.timeout = setTimeout(() => {
? ? ? ? ? func.apply(context, args)
? ? ? ? }, delay)
? ? ? }()// 注意:我加了()
? ? }Vue中的watch的防抖簡(jiǎn)寫(xiě)
? ? watchObj: {
? ? ? handler(val) {
? ? ? ? let _this = this
? ? ? ? clearTimeout(this.timeout)
? ? ? ? this.timeout = setTimeout(() => {
? ? ? ? ? _this.handlerData(val)
? ? ? ? }, 500)
? ? ? }
? ? }以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
詳解vue + vuex + directives實(shí)現(xiàn)權(quán)限按鈕的思路
這篇文章主要介紹了詳解vue + vuex + directives實(shí)現(xiàn)權(quán)限按鈕的思路,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10
vue頭部導(dǎo)航動(dòng)態(tài)點(diǎn)擊處理方法
這篇文章主要介紹了vue頭部導(dǎo)航動(dòng)態(tài)點(diǎn)擊處理方法,文中通過(guò)一段示例代碼給大家介紹了vue實(shí)現(xiàn)動(dòng)態(tài)頭部的方法,需要的朋友可以參考下2018-11-11
Vue項(xiàng)目引用百度地圖并實(shí)現(xiàn)搜索定位等功能(案例分析)
這篇文章主要介紹了Vue項(xiàng)目引用百度地圖并實(shí)現(xiàn)搜索定位等功能(案例分析),本篇文章為案例分析,技術(shù)點(diǎn)較多,所以篇幅較長(zhǎng),認(rèn)真閱覽的你一定會(huì)學(xué)到很多知識(shí),需要的朋友可以參考下2022-09-09
基于 Vue.js 2.0 酷炫自適應(yīng)背景視頻登錄頁(yè)面實(shí)現(xiàn)方式
本文講述如何實(shí)現(xiàn)擁有酷炫背景視頻的登錄頁(yè)面,瀏覽器窗口隨意拉伸,背景視頻及前景登錄組件均能完美適配,背景視頻可始終鋪滿窗口,前景組件始終居中,視頻的內(nèi)容始終得到最大限度的保留,可以得到最好的視覺(jué)效果2018-01-01
Vue?echarts@4.x中國(guó)地圖及AMap相關(guān)API使用詳解
這篇文章主要為大家介紹了Vue使用echarts@4.x中國(guó)地圖及AMap相關(guān)API使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12
Vue.js項(xiàng)目部署到服務(wù)器的詳細(xì)步驟
這篇文章給大家介紹了Vue.js項(xiàng)目部署到服務(wù)器的詳細(xì)步驟,既然是部署到服務(wù)器,肯定是需要一個(gè)云的。具體思路步驟大家可以參考下本文2017-07-07
Vue props 單向數(shù)據(jù)流的實(shí)現(xiàn)
這篇文章主要介紹了Vue props 單向數(shù)據(jù)流的實(shí)現(xiàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-11-11

