Javascript 按频率过滤输入(onoff debounce)

Javascript 按频率过滤输入(onoff debounce),javascript,node.js,io,raspberry-pi,rate-limiting,Javascript,Node.js,Io,Raspberry Pi,Rate Limiting,我使用raspberry pi上的节点和模块onoff来获取输入。 如果函数A在一分钟内被调用两次,我只想运行函数B var gpio = require('onoff').Gpio; var sensor = new gpio(7, 'in', 'falling'); var timeout = 60000; var timeOfLastChange; sensor.watch(function(err, value) { console.log('Sensor value is no

我使用raspberry pi上的节点和模块onoff来获取输入。 如果函数A在一分钟内被调用两次,我只想运行函数B

var gpio = require('onoff').Gpio;
var sensor = new gpio(7, 'in', 'falling');
var timeout = 60000;
var timeOfLastChange;
sensor.watch(function(err, value) {
    console.log('Sensor value is now '+ value);
    var currentTime = new Date(); //2 tick filter
    if(value == false && timeOfLastChange < currentTime - timeout) var timeOfLastChange = new Date();
    if(timeOfLastChange > currentTime - timeout) functionB();  
});
gpio.setup(7, gpio.DIR_IN);
var gpio=require('onoff').gpio;
var传感器=新的gpio(7,'英寸','下降');
var超时=60000;
最后变化时间;
传感器、手表(功能(错误、值){
console.log('传感器值现在为'+值);
var currentTime=new Date();//2勾选筛选器
if(value==false&&timeOfLastChangecurrentTime-timeout)函数b();
});
gpio.setup(7,gpio.DIR_-IN);

但这不起作用。

如果将日期计算更改为毫秒以匹配超时(int到int),则日期比较将起作用。大概是这样的:

timeOfLastChange.getTime() > (currentTime.getTime() - timeout)
currentTime = Date.now();
timeOfLastChange = Date.now();
或者更好的是,不要使用date对象,只使用epoch millis,如下所示:

timeOfLastChange.getTime() > (currentTime.getTime() - timeout)
currentTime = Date.now();
timeOfLastChange = Date.now();
然后,您的“时间”是一个类似整数的数字,与您的超时相匹配