For循环不适合函数
For loop does not work appropriate for the function
for循环包含一个函数,该函数通过checkPort函数指定端口是打开还是关闭。
var IPAdress = '192.168'; //Local area network to scan (this is rough)
var Portadd = 80;
var Newip;
var i=0;
var j=0;
//scan over a range of IP addresses and execute a function each time the port is shown to be open.
for(i=0; i <= 1; i++){
for(j=0; j <= 3; j++){
Newip = IPAdress+'.'+i+'.'+j;
checkPort(Portadd, Newip, function(error, status, host, port) {
// Status should be 'open' since the HTTP server is listening on that port
if(status == "open"){
console.log("IP" , Newip, "on port" , Portadd, "is open");
}
else if(status == "closed"){
console.log("IP" , Newip, "on port" , Portadd, "is closed");
}
});
console.log(Newip);
}
}
,结果如下:
192.168.0.0
192.168.0.1
192.168.0.2
192.168.0.3
192.168.1.0
192.168.1.1
192.168.1.2
192.168.1.3
IP 192.168.1.3 on port 80 is closed
IP 192.168.1.3 on port 80 is closed
IP 192.168.1.3 on port 80 is closed
IP 192.168.1.3 on port 80 is closed
IP 192.168.1.3 on port 80 is closed
IP 192.168.1.3 on port 80 is closed
IP 192.168.1.3 on port 80 is closed
IP 192.168.1.3 on port 80 is closed
因为打印出来的NewIp工作正常,所以我期望结果是这样的:
IP 192.168.0.0 on port 80 is closed
IP 192.168.0.1 on port 80 is closed
IP 192.168.0.2 on port 80 is closed
IP 192.168.0.3 on port 80 is closed
IP 192.168.1.0 on port 80 is closed
IP 192.168.1.1 on port 80 is closed
IP 192.168.1.2 on port 80 is closed
IP 192.168.1.3 on port 80 is closed
有没有人知道为什么在实际结果部分会显示ip ?
checkPort
使用稍后执行的回调函数,到执行时,变量NewIP
中的所有IP地址都已更改。
所以结果是回调函数打印NewIP
的最后一个值,因为它引用了这个值
你有两个选择:
- 您可以使用传递给回调函数的参数
host
代替。 - 通过改变
var Newip = IPAdress+'.'+i+'.'+j;
在内部for循环中定义var NewIP
。这将创建一个作用域为回调函数的变量。
更改为:
checkPort(Portadd, Newip, function(error, status, host, port) {
// Status should be 'open' since the HTTP server is listening on that port
if(status == "open"){
console.log("IP" , host, "on port" , port, "is open");
}
else if(status == "closed"){
console.log("IP" , host, "on port" , port, "is closed");
}
});
你不能传递" parent "变量到回调函数,特别是你有host
和port
输入参数
192.168.X范围内ip地址的完整示例。Y for X in [0,1,2] and Y in [0,1,2,3,4]:
var IPAdress = '192.168'; //Local area network to scan (this is rough)
var Portadd = 80;
var i=0;
var j=0;
//scan over a range of IP addresses and execute a function each time the port is shown to be open.
for(i=0; i <= 2; i++){
for(j=0; j <= 4; j++){
var Newip = IPAdress + '.' + i + '.' + j;
checkPort(Portadd, Newip, function(error, status, host, port) {
// Status should be 'open' since the HTTP server is listening on that port
if(status == "open"){
console.log("IP" , host, "on port" , port, "is open");
}else if(status == "closed"){
console.log("IP" , host, "on port" , port, "is closed");
}
});
console.log(Newip);
}
}
相关文章:
- Ember Data DS.Model's set函数不起作用
- jQuery加载的async和ready函数不工作
- 为什么构造函数不是构造函数
- 为什么我的JavaScript堆栈排序函数不起作用
- 参数更改时,角度表达式中的函数不更新
- Javascript onchange()函数不起作用
- jQuery函数不能只在一个页面上工作
- ResolveUrl没有'我似乎不适合当链接到具有动态虚拟目录的外部javascript文件时
- 另一个Ajax函数触发的Ajax函数不起作用
- javascript点击函数不;不适用于ID和Class
- 从PHP调用JS函数不起作用
- jQuery dosn'内联函数不能按预期工作
- Javascript函数不在deleteform上运行
- 如何使后缀函数不区分大小写
- setInterval函数不会更改视图中的$scope
- jQuery validate函数不起作用
- JS-窗口宽度函数不起作用
- javascript with()函数获胜'我不适合歌剧、Chrome或Brave
- 回调函数不适合HTML5视频播放器
- For循环不适合函数