[英]Variable restatement node.js
Here is a snippet from a node.js script I am working on. 这是我正在处理的node.js脚本的摘录。
I am using timers to emulate a tick-by-tick type of environment, in the beginning of the code I set the serverstatus to zero, in the startserver function I access the variable and attempt to reassign it to 1, but when the console.log appears, it reports that the variable is 0. 我使用计时器来模拟逐滴答录的环境,在代码的开头,我将serverstatus设置为零,在startserver函数中,我访问了变量并尝试将其重新分配为1,但是在控制台时。日志出现,它报告该变量为0。
setInterval(report,2000);
var server = setInterval(start, 4000);
var serverstatus = 0;
function report(){
console.log(serverstatus);
};
function start(){
if (serverstatus == 0){
var serverstatus = 1;
};
console.log("tick"")
};
What can I do to add one to that number and access it properly from the report function. 我该怎么做才能将一个数字添加到该数字并从报告功能中正确访问它。
Thank you! 谢谢!
Remove the var
from var serverstatus = 1;
从
var serverstatus = 1;
删除var
var serverstatus = 1;
so you are setting and testing the global variable by that name, not declaring a new, separate local variable. 因此您要使用该名称设置和测试全局变量,而不是声明一个新的单独的局部变量。
Working demo: http://jsfiddle.net/jfriend00/f9yuf7qq/ 工作演示: http : //jsfiddle.net/jfriend00/f9yuf7qq/
PS you also have a syntax error in console.log("tick"")
with an extra quote. PS您在
console.log("tick"")
也有语法错误,并带有额外的引号。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.