Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do I use lodash debounce? #117

Closed
AlexStacker opened this issue Jul 3, 2017 · 6 comments
Closed

How do I use lodash debounce? #117

AlexStacker opened this issue Jul 3, 2017 · 6 comments

Comments

@AlexStacker
Copy link

My code is as follows:

    handleWindowResize   = debounce(() => {
        console.log(this.screenWidth);
        this.screenWidth = window.innerWidth;
    }, 100)

    @Watch('screenWidth')
    onScreenWidthChange(newVal: number): void {
        console.log(newVal);
    }

But there is no work.
When I change my code like this,

    handleWindowResize(): void {
        console.log(this.screenWidth);
        this.screenWidth = window.innerWidth;
    }

    @Watch('screenWidth')
    onScreenWidthChange(newVal: number): void {
        console.log(newVal);
    }

It can change values from screenWidth. How do I organize this code with debounce?

@ktsn
Copy link
Member

ktsn commented Jul 3, 2017

Please use our forum or chat room to ask your questions. Issue list is reserved for bug reports or feature requests. Thanks.

@ktsn ktsn closed this as completed Jul 3, 2017
@304NotModified
Copy link
Contributor

@ktsn

The forum is down? Or is the link incorrect?

image

@ktsn
Copy link
Member

ktsn commented Jul 3, 2017

Oops, sorry. The link is incorrect.
https://forum.vuejs.org is the correct one.

@Robula
Copy link

Robula commented Jul 25, 2019

Since this issue is already here, why not leave an answer @ktsn ?

@NikitaKA
Copy link

NikitaKA commented Aug 6, 2019

#263 (comment)

@gbalduzzi
Copy link

gbalduzzi commented Jun 25, 2020

I found a solution here

First, you should define your not-debounced function:

handleWindowResize(): void {
     console.log(this.screenWidth);
     this.screenWidth = window.innerWidth;
}

Then, use debounce with this function instead of an anonymous one:

handleWindowResizeDebounced = debounce(this.handleWindowResize, 100)

Now you simply need to call handleWindowResizeDebounced and you will have your debounced function without the need of an additional dependency

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants