- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有控制 WS2812b strip 的流程。我正在使用将 bool 变量发送到以下函数的开关,其中我获取全局变量,其中我将当前颜色存储在 HSL 中。关闭(false)脚本后加载当前颜色并将“L”更改为 0(工作),但是当我再次打开它时(true)它再次获得 L=0,即使我没有在任何地方更改它。
if(msg.payload === false)
{
col = global.get("color");
col.l = 0;
}
if(msg.payload === true)
{
col = global.get("color");
}
msg.payload = col;
return msg;
流程:
[{"id":"79df9203.a409ec","type":"tab","label":"Obyvací pokoj","disabled":false,"info":""},{"id":"431fac94.09cba4","type":"mqtt out","z":"79df9203.a409ec","name":"","topic":"/TV_LED/cmd","qos":"","retain":"","broker":"e17662db.a0a03","x":1100,"y":140,"wires":[]},{"id":"a31190b1.8af6c","type":"function","z":"79df9203.a409ec","name":"hsl > neopixel","func":"var hslToRgb = function(hue, saturation, lightness){\n // based on algorithm from http://en.wikipedia.org/wiki/HSL_and_HSV#Converting_to_RGB\n if( hue == undefined ){\n return [0, 0, 0];\n }\n\n var chroma = (1 - Math.abs((2 * lightness) - 1)) * saturation;\n var huePrime = hue / 60;\n var secondComponent = chroma * (1 - Math.abs((huePrime % 2) - 1));\n\n huePrime = Math.floor(huePrime);\n var red;\n var green;\n var blue;\n\n if( huePrime === 0 ){\n red = chroma;\n green = secondComponent;\n blue = 0;\n }else if( huePrime === 1 ){\n red = secondComponent;\n green = chroma;\n blue = 0;\n }else if( huePrime === 2 ){\n red = 0;\n green = chroma;\n blue = secondComponent;\n }else if( huePrime === 3 ){\n red = 0;\n green = secondComponent;\n blue = chroma;\n }else if( huePrime === 4 ){\n red = secondComponent;\n green = 0;\n blue = chroma; \n }else if( huePrime === 5 ){\n red = chroma;\n green = 0;\n blue = secondComponent;\n }\n\n var lightnessAdjustment = lightness - (chroma / 2);\n red += lightnessAdjustment;\n green += lightnessAdjustment;\n blue += lightnessAdjustment;\n \n return {\n r: Math.round(red * 255),\n g: Math.round(green * 255),\n b: Math.round(blue * 255)\n };\n}\nnode.warn(msg.payload);\nmsg.payload = hslToRgb(msg.payload.h,msg.payload.s,msg.payload.l);\nmsg.payload = \"Neopixelall,\"+msg.payload.r+\",\"+msg.payload.g+\",\"+msg.payload.b;\nreturn msg;\n\n","outputs":1,"noerr":0,"x":920,"y":140,"wires":[["431fac94.09cba4"]]},{"id":"5e030bab.a9e354","type":"mqtt in","z":"79df9203.a409ec","name":"","topic":"/TV_LED/color","qos":"2","broker":"e17662db.a0a03","x":300,"y":340,"wires":[["d5993f0b.74a52"]]},{"id":"d5993f0b.74a52","type":"ui_text","z":"79df9203.a409ec","group":"aec2f80b.a31aa8","order":5,"width":0,"height":0,"name":"","label":"Stored color:","format":"{{msg.payload}}","layout":"row-spread","x":490,"y":340,"wires":[]},{"id":"30c421ea.7900fe","type":"mqtt in","z":"79df9203.a409ec","name":"","topic":"/TV_LED/color","qos":"2","broker":"e17662db.a0a03","x":160,"y":140,"wires":[["90df2140.97555"]]},{"id":"4ac39175.f01b8","type":"mqtt out","z":"79df9203.a409ec","name":"","topic":"/TV_LED/color","qos":"","retain":"true","broker":"e17662db.a0a03","x":1100,"y":100,"wires":[]},{"id":"c2735b8.0f402a8","type":"link out","z":"79df9203.a409ec","name":"LED_TV","links":["5db90cc7.188e14"],"x":735,"y":120,"wires":[]},{"id":"5db90cc7.188e14","type":"link in","z":"79df9203.a409ec","name":"","links":["c2735b8.0f402a8"],"x":775,"y":120,"wires":[["a31190b1.8af6c","e53fc48c.f09088"]]},{"id":"90df2140.97555","type":"function","z":"79df9203.a409ec","name":"string>light","func":"msg.payload= JSON.parse(msg.payload);\nglobal.set(\"color\",msg.payload);\n\nmsg.payload = msg.payload.l;\nreturn msg;\n","outputs":1,"noerr":0,"x":330,"y":140,"wires":[["5dec6394.05474c"]]},{"id":"5dec6394.05474c","type":"ui_slider","z":"79df9203.a409ec","name":"","label":"Lightness","tooltip":"","group":"aec2f80b.a31aa8","order":5,"width":0,"height":0,"passthru":false,"outs":"end","topic":"","min":0,"max":"1","step":"0.1","x":480,"y":140,"wires":[["47416ab5.d30f44"]]},{"id":"f0d4dbfb.267d68","type":"ui_colour_picker","z":"79df9203.a409ec","name":"","label":"","group":"aec2f80b.a31aa8","format":"hsl","outformat":"object","showSwatch":true,"showPicker":false,"showValue":false,"showHue":false,"showAlpha":false,"showLightness":false,"dynOutput":"false","order":8,"width":0,"height":0,"passthru":true,"topic":"","x":470,"y":80,"wires":[["c2735b8.0f402a8"]]},{"id":"e53fc48c.f09088","type":"function","z":"79df9203.a409ec","name":"not black","func":"if(msg.payload.l !== 0)\n return msg; \n","outputs":1,"noerr":0,"x":900,"y":100,"wires":[["4ac39175.f01b8"]]},{"id":"94fe53c2.444ea","type":"ui_switch","z":"79df9203.a409ec","name":"","label":"On / Off","tooltip":"","group":"aec2f80b.a31aa8","order":8,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","x":480,"y":240,"wires":[["1a00a4b.e0dbe5b"]]},{"id":"47416ab5.d30f44","type":"function","z":"79df9203.a409ec","name":"light>hsl","func":"var col = global.get(\"color\");\ncol.l = msg.payload;\nmsg.payload = col;\nreturn msg;\n","outputs":1,"noerr":0,"x":620,"y":140,"wires":[["c2735b8.0f402a8"]]},{"id":"1a00a4b.e0dbe5b","type":"function","z":"79df9203.a409ec","name":"light off","func":"if(msg.payload === false)\n{\n col = global.get(\"color\");\n col.l = 0;\n}\n \nif(msg.payload === true)\n{\n col = global.get(\"color\");\n}\nmsg.payload = col;\nreturn msg;","outputs":1,"noerr":0,"x":620,"y":240,"wires":[["c2735b8.0f402a8"]]},{"id":"e17662db.a0a03","type":"mqtt-broker","z":"","name":"Broker","broker":"localhost","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"aec2f80b.a31aa8","type":"ui_group","z":"","name":"LED - TV","tab":"5b27353d.d34fbc","order":3,"disp":true,"width":"4","collapse":false},{"id":"5b27353d.d34fbc","type":"ui_tab","z":"","name":"Obývací pokoj","icon":"dashboard","order":1,"disabled":false,"hidden":false}]
编辑:我不知道这是否是 JS 的正确行为,但这是一个小调试:
col = global.get("color");
global.set("ori_color",col);
node.warn(global.get("ori_color")); //1
col.l = 0;
node.warn(global.get("ori_color")); //2
msg.payload = col;
调试:1) 高:300,短:1,长:0.52) 高:300,小:1,长:0
所以它基本上通过 col
变量回顾性地改变全局变量..奇怪
最佳答案
In JavaScript objects are passed and assigned by reference .这意味着您必须将新对象分配给 col
变量(复制)。
if(msg.payload === false)
{
var col = Object.assign({}, global.get("color"));
col.l = 0;
msg.payload = col;
}
else
col = global.get("color");
msg.payload = col;
return msg;
关于javascript - Node-Red:覆盖的全局变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54612417/
我的应用程序中有一个 settings.php 页面,它使用 $GLOBALS 来存储网络应用程序中使用的配置。 例如,他是我使用的一个示例设置变量: $GLOBALS["new_login_page
我正在尝试编译我们在 OS 类上获得的简单操作系统代码。它在 Ubuntu 下运行良好,但我想在 OS X 上编译它。我得到的错误是: [compiling] arch/i386/arch/start
我知道distcp无法使用通配符。 但是,我将需要在更改的目录上安排distcp。 (即,仅在星期一等“星期五”目录中复制数据),还从指定目录下的所有项目中复制数据。 是否有某种设计模式可用于编写此类
是否可以在config.groovy中全局定义资源格式(json,xml)的优先级,而不是在每个Resource上指定?例如,不要在@Resource Annotation的参数中指定它,例如: @R
是否有一些简单的方法来获取大对象图的所有关联,而不必“左连接获取”所有关联?我不能只告诉 Hibernate 默认获取 eager 关联吗? 最佳答案 即使有可能有一个全局 lazy=false(谷歌
我正在尝试实现一个全局加载对话框...我想调用一些静态函数来显示对话框和一些静态函数来关闭它。与此同时,我正在主线程或子线程中做一些工作...... 我尝试了以下操作,但对话框没有更新...最后一次,
当我偶然发现 this question 时,我正在阅读更改占位符文本。 无论如何,我回去学习了占位符。一个 SO 的回答大致如下: Be careful when designing your pl
例如,如果我有这样的文字: "hello800 more text 1234 and 567" 它应该匹配 1234 和 567,而不是 800(因为它遵循 hello 的 o,这不是一个数字)。 这
我一直在尝试寻找一种无需使用 SMS 验证系统即可验证电话号码(Android 和 iPhone)的方法。原因纯粹是围绕成本。我想要一个免费的解决方案。 我可以安全地假设 Android 操作系统会向
解决此类问题的规范 C++ 设计模式是什么? 我有一些共享多个类的多线程服务器。我需要为大多数类提供各种运行时参数(例如服务器名称、日志记录级别)。 在下面的伪 C++ 代码中,我使用了一个日志记录类
这个问题在这里已经有了答案: Using global variables in a function (25 个答案) 关闭 9 年前。 我是 python 的新手,所以可能有一个简单的答案,但我
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: Does C++ call destructors for global and class static
我正在尝试使用 Objective-C 中的 ArrayList 的等价物。我知道我必须使用 NSMutableArray。我想要一个字符串列表 (NSString)。关键是我的列表应该可以从我类(c
今天刚开始学习 Android 开发,我找不到任何关于如何定义 Helper 类或将全局加载的函数集合的信息,我会能够在我创建的任何 Activity 中使用它们。 我的计划是创建(至少目前)2 个几
为什么这段代码有效: var = 0 def func(num): print num var = 1 if num != 0: func(num-1) fun
$GLOBALS["items"] = array('one', 'two', 'three', 'four', 'five' ,'six', 'seven'); $alter = &$GLOBALS
我想知道如何实现一个可以在任何地方使用您自己的设置的全局记录器: 我目前有一个自定义记录器类: class customLogger(logging.Logger): ... 该类位于一个单独的
我需要使用 React 测试库和 Jest 在我的测试中模拟不同的窗口大小。 目前我必须在每个测试文件中包含这个beforeAll: import matchMediaPolyfill from 'm
每次我遇到单例模式或任何静态类(即(几乎)只有静态成员的类)的实现时,我想知道这是否实际上不是一种黑客行为,因此只是为了设计而严重滥用类和实例的原则单个对象,而不是设计类和创建单个实例。对我来说,看起
这个问题在这里已经有了答案: Help understanding global flag in perl (2 个回答) 7年前关闭。 my $test = "There was once an\n
我是一名优秀的程序员,十分优秀!