如何在node.js中更改布尔变量中的消息

问题描述 投票:0回答:1

我正在尝试修改代码,因此当LED打开和关闭时,程序将显示Active-Inactive消息,而不是经典布尔值True-false。

我目前在Beaglebone Black中使用这个代码,我是Node.js中的新手,我在一本书(探索BeagleBone)中找到了它,当我按照它在书中运行它时,它显示了以下消息: Led is: true, Led is: false,但当我尝试修改它以显示活动 - 非活动时,它不起作用。

function toggleLED()
{
    isOn = !isOn    // invert the isOn state in each call
    if (isOn) b.digitalWrite(LED3Pin, 1); // light the led
    else b.digitalWrite(LED3Pin, 0); // turn off the led
    if (isOn) isOn = 'active'; // changing default TRUE message for active
    else isOn = 'inactive'; // changing default FALSE message for inactive
    console.log('LED is: ' + isOn); // show the state
}

我希望这段代码的输出为:LED is: active, LED is: inactive, LED is: active, LED is: inactive,依此类推,但实际输出只显示第一个为active,其余为inactive

javascript node.js beagleboneblack
1个回答
0
投票
function toggleLED()
{
    isOn = !isOn    // invert the isOn state in each call
    if (isOn) b.digitalWrite(LED3Pin, 1); // light the led
    else b.digitalWrite(LED3Pin, 0); // turn off the led
    console.log('LED is: ' + isOn ? 'active' : 'inactive'); // show the state
}

您将变量isOn从布尔值truefalse更改为字符串"active""inactive",这将在下一次调用时被视为true。这就是为什么其余的都将是inactive

© www.soinside.com 2019 - 2024. All rights reserved.