1

Currently, im trying to call a throttle/debounce function in my Vue component, but every time it's called a Uncaught TypeError: functionTD is not a function si throw here is my code.

useThrottleDebounce.ts

import { debounce, throttle } from "lodash";
import { ref, watch } from "vue";

export const useThrottleDebounce = (tTime = 2000, dTime = 1000) => {
  const tRef = ref<any>(null);
  const tFunc = ref<any>(null);
  const tDHook = ref<any>(null);

  const debounceThrottle = debounce(() => {
    if (tRef.value) {
      tRef.value.cancel();
    }
    tRef.value = throttle(tFunc.value, tTime)();
  }, dTime);

  const throttleDebounceCreator = () => {
    return (func: any) => {
      tFunc.value = func;
      debounceThrottle();
    };
  };

  watch(() => tDHook.value, () => {
    tDHook.value = throttleDebounceCreator();
  });

  return tDHook;
};

export default useThrottleDebounce;

and this is when it's called inside setup in my component

setup(){
   // some code
   const functionTD = useThrottleDebounce(2000, 500);
   const inc = () => {      
      functionTD (() => {     
        count.value++; // here throw error
      });
    };
}
4
  • const function = useThrottleDebounce(2000, 500); is illegal JavaScript. You can't name a constant any of the reserved words in JavaScript and function is one of them. If you need help please provide a runnable minimal reproducible example, including implementation and what you're trying to achieve. Commented Feb 11, 2021 at 23:54
  • i just update code function name Commented Feb 11, 2021 at 23:55
  • That's not enough. You're not showing how you use it and you're not describing what should happen vs what currently happens. If I run the code above, I don't get the error you describe. How are you using your function? Commented Feb 11, 2021 at 23:56
  • @tao the function is using as is shown in the last code snippet Commented Feb 12, 2021 at 0:48

1 Answer 1

1

The issue is that useThrottleDebounce doesn't return a function, therefore functionTD is not a function:

export const useThrottleDebounce = (tTime = 2000, dTime = 1000) => {
  // Maybe you want some private variables / functions here
  return () => {
    // This will be `functionTD` in `setup`
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.