今天在优化代码的时候 ,想给触摸事件设置一个节流,利用setTimeout来设置一个延缓事件,但是发现不起作用 代码是这样的
this.timer = setTimeout(function() {
const touchY = e.touches[0].clientY - 80
const index = Math.floor((touchY - this.startY) / 22)
if (index >= 0 && index < this.letters.length) {
this.$emit('change', this.letters[index])
}
}, 16)
然后打印了下数据,发现变成NaN了,于是我试着把function()换成了()=> ,就是下面这样
this.timer = setTimeout(() => {
const touchY = e.touches[0].clientY - 80
const index = Math.floor((touchY - this.startY) / 22)
if (index >= 0 && index < this.letters.length) {
this.$emit('change', this.letters[index])
}
}, 16)
就起作用了, 感觉应该是this的指向发生了变化