最近做一个微信小程序图片懒加载的方法,在onPageScroll事件中直接添加了节流函数并执行,但是并没有节流,一顿检查测试节流函数和自己需要节流的函数都是没有问题,居然在节流函数中没有走return函数,因此没有调用成功,开始时这么写的,按照普通正常的操作:

节流函数:

export function throttle(fn, gaptime) { 
	if (gaptime == null || gaptime == undefined) { 
		gaptime = 200
	}
	let _lastTime = null
	return function() { 
		let _nowTime = +new Date()
		if (_nowTime - _lastTime > gaptime || !_lastTime) { 
			fn.apply(this, arguments)
			_lastTime = _nowTime
		}
	}
}
onPageScroll() { 
  throttle(this.showImg,100)				
},

解决方案

其实自己用法没用对,本身onPageScroll就是一个函数
修改

onPageScroll: throttle(function() { 
	this.showImg()				
}, 100)

原文链接:https://blog.csdn.net/weixin_42164539/article/details/106875415

本站声明:网站内容来源于网络,如有侵权,请联系我们,我们将及时处理。

本博客所有文章如无特别注明均为原创。
复制或转载请以超链接形式注明转自起风了,原文地址《uni-app微信小程序onPageScroll事件中使用节流函数无效解决方案
   

还没有人抢沙发呢~