我有一个函数已经将一个参数绑定到一个监听器回调。它工作得很好,但现在我需要去抖(lodash)那个
我似乎无法正确获取debounce绑定,以便将参数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)
}
但是得到错误,我的绑定debounce不是一个函数,而是根据日志语句。
[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'绑定到debounce的函数?
答案 0 :(得分:0)
永远不要忘记提供第一个context / this参数来绑定,即使是null。
return debounce.bind(null,processor.bind(this,pin),wait, options)