Jquery移动/ JavaScript问题
我无法通过数据的各种值正确循环“获取”,它应该发送
... CMD = PC&安培;数据= 06464ff
... CMD = PC&安培;数据= 16464ff
... CMD = PC&安培;数据= 26464ff
i = 0;
do {
setTimeout(function () {
$.get('send.php', {
ip: ip,
id: id,
port: port,
cmd: "PC",
data: i + "6464ff"
}).done(function (response) {
if (response.indexOf("PC") === 0) {
response = response.replace("PC", "");
} else {
$("#ERROR").html(response);
response = currentvm;
}
});
}, 2000 * i);
i++;
}
while (i < 255);
我得到的只是
... CMD = PC&安培;数据= 2556464ff
... CMD = PC&安培;数据= 2556464ff
有关如何继续的任何想法或建议。
谢谢。
答案 0 :(得分:0)
使用闭包应解决您的问题:
do {
(function (i) {
setTimeout(function () {
$.get('send.php', {
ip: ip,
id: id,
port: port,
cmd: "PC",
data: i + "6464ff"
}).done(function (response) {
if (response.indexOf("PC") === 0) {
response = response.replace("PC", "");
} else {
$("#ERROR").html(response);
response = currentvm;
}
});
}, 2000 * i);
}(i));
i++;
}
while (i < 255);
答案 1 :(得分:-1)
可能的选择: 尝试更改&#39; send.php&#39; by&#39; send.php?&#39; + i +&#34; 6464ff&#34;
或
$.get('send.php', {
ip: ip,
id: id,
port: port,
cmd: "PC",
data: i + "6464ff",
success: function(response) {
if (response.indexOf("PC") === 0) {
response = response.replace("PC", "");
}
else {$("#ERROR").html(response);
response = currentvm;
}
});