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

debounce示例写法有点欠妥 #194

Open
Bjkb opened this issue Jul 20, 2019 · 0 comments
Open

debounce示例写法有点欠妥 #194

Bjkb opened this issue Jul 20, 2019 · 0 comments

Comments

@Bjkb
Copy link

Bjkb commented Jul 20, 2019

image
测试将immediate置为false,后面的函数变量没有被保存

测试用例:

function testFn(name){
  console.log(`hello ${name}`);
}
var d = debounce(testFn,0,false);
d('xiaomin');
d('xiaohua');
-----> hello xiaomin  // 置为false,应该输出 hello xiaohua

稍加修改

 return function(...params) {
     // 否则缓存参数和调用上下文
     // 更改的地方
     context = this
     args = params
    // 如果没有创建延迟执行函数(later),就创建一个
    if (!timer) {
      timer = later()
      // 如果是立即执行,调用函数
      if (immediate) {
        func.apply(this, params)
      }
    // 如果已有延迟执行函数(later),调用的时候清除原来的并重新设定一个
    // 这样做延迟函数会重新计时
    } else {
      clearTimeout(timer)
      timer = later()
    }
  }

// 测试用例输出 -----> hello xiaohua
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

1 participant