【问题标题】:How to make clearTimeout work properly to cancel(break) setTimeout loop?如何使 clearTimeout 正常工作以取消(中断)setTimeout 循环?
【发布时间】:2019-03-26 06:04:37
【问题描述】:

我正在使用 Node.js 构建一个可以读取 USB LoRa 模块数据并控制 Raspberry Pi GPIO 引脚的应用程序。

主要有三个功能:

  1. 设置 GPIO 开启(给定引脚 0 信号)

  2. 关闭 GPIO(给定引脚 1 信号)

  3. 设置GPIO在一段时间后关闭(使用setTimeout函数)

主要功能运行良好。

问题是在setTimeout循环中,我想随时终止它。

我尝试使用clearTimeout 取消setTimeout 循环,但它似乎不起作用。我做错了吗?

例如。

在 LoRa 接收到的数据包中使用字符串“TurnOn15s”。 我无法随时终止超时。我必须等到 15 秒结束才能关闭 GPIO 引脚。如果我在超时执行期间调用 deviceTurnOff(1) 函数,我会在循环结束后得到两个输出。

程序输出如下:

// Output by `deviceTurnOn(0,15000)` from received LoRa packet.
Received:0, the solenoid valve has been opened!                                 
Received packet: !20!96 0 0 0 53 0 0 28 11 3 TurnOn15s RSSI: -40 SNR:   6

// Output by `setTimeout` function.
Received:1, the solenoid valve has been closed!                                 

// Output by deviceTurnOff(1) from received LoRa packet.
Received:1, the solenoid valve has been closed!                                 
Received packet: !18!96 0 0 0 53 0 0 29 9 3 TurnOff RSSI: -41 SNR:   7 

这里是部分代码:

var turnOffTimeout = null; // turnOffTimeout

// Control Raspberry Pi GPIO pins.
function deviceTurnOn(controlValueType, timeout) {

  // Do Turn On Raspberry Pi GPIO pins.
  if (timeout == -1) {

    // if no time string in the packet.
    console.log(`Received:${controlValueType}, the solenoid valve has been opened!`);
    OutputDevice.writeSync(controlValueType);
  } else {

    // if contains time string in the packet.
    console.log(`Received:${controlValueType}, the solenoid valve has been opened!`);
    OutputDevice.writeSync(controlValueType);
    turnOffTimer(timeout);
  }
}

// Control Raspberry Pi GPIO pins.
function deviceTurnOff(controlValueType) {

  // Do Turn Off Raspberry Pi GPIO pins.
  console.log(`Received:${controlValueType}, the solenoid valve has been closed!`);
  OutputDevice.writeSync(controlValueType);
  // clearTimeout
  if (turnOffTimeout) {
    clearTimeout(turnOffTimeout);
  }
}


// Raspberry Pi GPIO pins turn off timeout.
function turnOffTimer(timeout) {
  turnOffTimeout = setTimeout(function() {
    deviceTurnOff(1);
  }, timeout);
}

包含三个功能: deviceTurnOn、deviceTurnOff、turnOffTimer

函数调用的格式是

deviceTurnOff(1);
deviceTurnOn(0,timeout);

// Timeout is milliseconds

更新;

我尝试修改我的程序如下,得到的输出和之前版本的代码一样,问题还是没有解决。

 var turnOffTimeout = null; // turnOffTimeout

    // Control Raspberry Pi GPIO pins.
    function deviceTurnOn(controlValueType, timeout) {
        // Do Turn On Raspberry Pi GPIO pins.

        if (timeout == -1) {

            // if no time string in the packet.
            console.log(`Received:${controlValueType}, the solenoid valve has been opened!`);
            OutputDevice.writeSync(controlValueType);
        }
        else {

            // if contains time string in the packet.
            console.log(`Received:${controlValueType}, the solenoid valve has been opened!`);
            OutputDevice.writeSync(controlValueType);
            // clearTimeout.
            clearTimeout(turnOffTimeout);
            // set new timeout.
            turnOffTimeout = setTimeout(function () {
                deviceTurnOff(1);
            }, timeout);
        }
    }

    // Control Raspberry Pi GPIO pins.
    function deviceTurnOff(controlValueType) {

        // Do Turn Off Raspberry Pi GPIO pins.
        console.log(`Received:${controlValueType}, the solenoid valve has been closed!`);
        clearTimeout(turnOffTimeout);
        OutputDevice.writeSync(controlValueType);

    }

【问题讨论】:

  • “但它似乎不起作用”——你看到了什么行为?
  • 在设置新的超时之前,您似乎没有清除超时。结果,您有一个不再被变量引用的超时,同时给您多个超时。将变量设置为新的时,请务必清除现有的超时。
  • @brian-lives-outdoors 例如,超时设置为 15(秒)。调用 deviceTurnOff(1)。执行循环时,我必须等待 15(秒)才能关闭 GPIO。 15(秒)后,同时得到两个输出,一个是setTimeout提供的,另一个是deviceTurnOff(1)调用的结果。
  • @MarkMeyer 谢谢提醒,我没注意这个,我试试。

标签: javascript node.js raspberry-pi settimeout cleartimeout


【解决方案1】:

确保在启动和停止时清除超时,您也不需要检查是否设置了超时。此外,真的不需要第三个函数来取消计时器,因为clearTimeout 已经存在:

var timeout;
var timeout_ms = 10000;

var stop = function(){
 clearTimeout(timeout);
 turnOff();
}

var start(){
 clearTimeout(timeout); // clear existing t/o just in case
 timeout = setTimeout(turnOff,timeout_ms);
 turnOn();
}

【讨论】:

  • 您好,按照您建议的方式试过了,还是不行,更新原帖中的代码,是不是代码有其他问题?
  • 什么是controlValueType?为什么你没有openSolenoid() 和closeSolenoid() - 根据你的代码,deviceTurnOn 和deviceTurnOff 之间没有区别,它们都只是使用OutputDevice.writeSync(controlValueType);
  • 你好:controlValueType只有0和1之差。参考你的建议,尽量简化代码,但还是不行,我无法随时终止setTimeout循环(意思是我不能随时关闭GPIO设备,我要等到超时结束)这里附上所有代码,有没有其他的想法? Https://jsbin.com/vimawifutu/1/edit?js
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-06-26
  • 1970-01-01
  • 1970-01-01
  • 2021-11-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多