Skip to content

useDebounce #345

@murongg

Description

@murongg
Contributor

Info

Basic info of your challenge questions,

difficulty: hard
title: useDebounce
tags: Composable Function

Question

For this challenge, you need implement a debounce Composable Function. Let's go.

import type { Ref} from 'vue'

interface UseDebounceOptions {
  leading?: boolean // Specify invoking on the leading edge of the timeout.
  maxWait?: number // The maximum time func is allowed to be delayed before it's invoked.
  trailing?: boolean // Specify invoking on the trailing edge of the timeout.
}

type MaybeRef<T> = T | Ref<T>
type UseDebounce = <T extends (...args: any[]) => any>(fn: T, wait: MaybeRef<number>, options?: UseDebounceOptions) => T

/**
 * useDebounce
 * @param fn The function to debounce.
 * @param wait The number of milliseconds to delay.
 * @param options The options object.
 * @return Returns the new debounced function.
 */
const useDebounce: UseDebounce = (fn, wait, options) => {
  // do someting...
}

Template

filename: useDebounce.ts

import type { Ref} from 'vue'

interface UseDebounceOptions {
  leading?: boolean // Specify invoking on the leading edge of the timeout.
  maxWait?: number // The maximum time func is allowed to be delayed before it's invoked.
  trailing?: boolean // Specify invoking on the trailing edge of the timeout.
}

type MaybeRef<T> = T | Ref<T>
type UseDebounce = <T extends (...args: any[]) => any>(fn: T, wait: MaybeRef<number>, options?: UseDebounceOptions) => T

/**
 * useDebounce
 * @param fn The function to debounce.
 * @param wait The number of milliseconds to delay.
 * @param options The options object.
 * @return Returns the new debounced function.
 */
const useDebounce: UseDebounce = (fn, wait, options) => {
  // do someting...
}

Activity

added a commit that references this issue on Jul 7, 2022
linked a pull request that will close this issue on Jul 7, 2022
github-actions

github-actions commented on Jul 7, 2022

@github-actions
Contributor

#347 - Pull Request updated.

2022-07-07T07:01:43.933Z Preview in Playground

added a commit that references this issue on Jul 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @murongg

      Issue actions

        useDebounce · Issue #345 · webfansplz/vuejs-challenges