I
I
Ilya Kunovskiy2021-03-22 16:37:31
Vue.js
Ilya Kunovskiy, 2021-03-22 16:37:31

How to import js listener into VueJS component?

Hello, the question arose, there is throttle on listening to the scroll in the JS file, how can I pass it to App.vue, so that with its help the variable in date

throttle.js changes when scrolling:

function throttle(func, ms) {
  let isThrottled = false;

  function wrapper() {

    if(isThrottled) {
      return;
    }

    func.apply(this, arguments);

    isThrottled = true;

    setTimeout(function() {
      isThrottled = false;
    }, ms);
  }

  return wrapper;
}

window.addEventListener('scroll', throttle(function scroll(p) {
  p = p + 1;
  console.log(p);
}, 1000))

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rsa97, 2021-03-22
@roofmorozov

Move the throttle function code to the application.
Hang the scroll handler in the mounted hook, remove it in beforeDestroy.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question