0

我有一个已经将参数绑定到侦听器回调的函数。它工作得很好,但现在我需要去抖动(lodash)

我似乎无法正确绑定去抖动,以便将参数pin_num传递给侦听器回调的 .bind 。

这是我尝试过的

const pinDebounce = function (processor, wait, options, pin) {
  console.log(' ',processor)  // confirm it's a function
  return debounce.bind(processor.bind(this,pin),wait, options)
}

for(const pin_num in this.interrupts) {
  let pin = this.interrupts[pin_num].pin
  let edge = this.interrupts[pin_num].edge
  console.log(`yy starting interrupt on pin ${pin_num} with debounce wait/max:${this.wait},${this.maxwait}`)
  // what I had as the cb to .on which works fine
  // this.interruptProcess.bind(this,pin_num)
  // now wrap that in a debounce
  let pd = pinDebounce(this.interruptProcess,this.wait,{ 'maxWait': this.maxwait },pin_num)
  console.log(' ',pd)  // confirm it's a function
  pin.on('interrupt', pd )
  console.log('edge=',edge)
  if(!this.mock) pin.enableInterrupt(edge)
}

但是收到错误,我的绑定去抖动不是一个函数,而是根据日志语句它是。

[INTERRUPT-ERROR] TypeError: Expected a function
    at Function.debounce (/opt/uci/uci-examples/node_modules/lodash.debounce/index.js:144:11)
    at Gpio.emit (events.js:160:13)

https://github.com/lodash/lodash/blob/master/debounce.js

尝试了一些变化,但没有爱。所以一定没有什么完全正确的。请帮忙

或者,有没有更好的方法让我的“pin_num”绑定到传递给去抖动的函数?

4

1 回答 1

0

永远不要忘记提供第一个 context/this 参数来绑定,即使为 null。

return debounce.bind(null,processor.bind(this,pin),wait, options)
于 2018-02-27T05:37:24.340 回答