Skip to content

debounce #1

Description

@caiyongqi

防抖函数debounce在setTimeout中通过apply执行的函数(debounce第一个参数)中的this(apply第一个参数)测试为undefined,感觉应该在debounce中传入一个'this'(项目中为this.$refs.scroll)
// 防抖函数 export function debounce(func, that = null, delay = 500) { // 回调函数,和回调函数要执行须等待的时间ms let timer = null // this => Vue组件 return function (...args) { // this => undefined if (timer) { clearTimeout(timer) } timer = setTimeout(() => { // 测试证明此处使用this => undefined func.apply(that, args) //func() }, delay) } }

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions