[Vuejs]-I want to create a variable to prevent user repeat click but it doesn't work

0πŸ‘

βœ…

I find the reason!It because I set a EventListener and I don’t remove it.Here is my revised code:

function clear() {
  isClick = true
  clearInterval(scrollToAnimation)
  scrollToAnimation = null
  per_distance = null
  top_distance = null
  per_time = 0
}

function monitorWheel() {
  window.removeEventListener('scroll', monitorScroll)
  if (docEl.scrollTop == location) {
    clear()
    window.removeEventListener('wheel', monitorWheel)
    window.removeEventListener('scroll', monitorScroll)
  } else {
    setTimeout(() => {
      clear()
      window.removeEventListener('wheel', monitorWheel)
      window.removeEventListener('scroll', monitorScroll)
    }, per_time * 20)
  }
}

function monitorScroll() {
  if (docEl.scrollTop == location) {
    clear()
    window.removeEventListener('wheel', monitorWheel)
    window.removeEventListener('scroll', monitorScroll)
  } else {
    setTimeout(() => {
      clear()
      window.removeEventListener('wheel', monitorWheel)
      window.removeEventListener('scroll', monitorScroll)
    }, per_time * 20)
  }
}
window.addEventListener('wheel', monitorWheel)
window.addEventListener('scroll', monitorScroll)

After this modify,when the scroll animate is over,the EventListener will be remove,then question is solved!

Leave a comment