基本上,我正在编码一个代码,该代码从textarea提取第一行,然后将其发送到php代码并获取响应。但是,每当我将数据放入浏览器停止工作时,都不会发生这种情况,这是我正在使用的代码
function getNewSocks() {
socksList.splice(0, 1);
$('#socks').val(socksList.join("\n"));
}
var socksList = $("#socks").val().split("\n");
while (socksList.length) {
var getTheSocksLine = $.trim(socksList[0].replace(/\s+/g, ''));
var getIP = $.trim(getTheSocksLine.split(":")[0]);
var getPort = $.trim(getTheSocksLine.split(":")[1]);
if (typeof getIP == "undefined" || typeof getPort == "undefined" || getIP == "" || getPort == "") {
alert("typeof");
socksDieResultDisplay(getIP + ":" + getPort);
getNewSocks();
}
else if (getTheSocksLine.match(/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:\d{1,6}$/) == null) {
alert("error");
socksDieResultDisplay(getIP + ":" + getPort);
getNewSocks();
}
else {
$.ajax({
url: "system/checkerPostHandler.php",
method: "POST",
data: "checkSocks=CheckSocksNowPlease&ip=" + getIP + "&port=" + getPort,
success: function (getResponse) {
alert("PHP Response=>" + getResponse + " jQuery Response=>" + getIP);
if (socksList.length != 0) {
getNewSocks();
}
}
});
}
}
我向上帝发誓,现在已经花了3-4个小时试图找出问题所在!请帮帮我
最佳答案
您可以引入一个计数器,并用socksList.length
将其映射到<=
,然后循环将运行,否则将退出。
您的代码的问题是循环条件将始终为true,并且它将始终运行。如果socksList.length
不是socksList.length
,则socksList.length
条件将始终为true。
例。
function getNewSocks() {
socksList.splice(0, 1);
$('#socks').val(socksList.join("\n"));
}
var socksList = $("#socks").val().split("\n");
let counter = 0;
while (counter <= socksList.length) {
var getTheSocksLine = $.trim(socksList[counter].replace(/\s+/g, ''));
var getIP = $.trim(getTheSocksLine.split(":")[0]);
var getPort = $.trim(getTheSocksLine.split(":")[1]);
if (typeof getIP == "undefined" || typeof getPort == "undefined" || getIP == "" || getPort == "") {
alert("typeof");
socksDieResultDisplay(getIP + ":" + getPort);
getNewSocks();
}
else if (getTheSocksLine.match(/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:\d{1,6}$/) == null) {
alert("error");
socksDieResultDisplay(getIP + ":" + getPort);
getNewSocks();
}
else {
$.ajax({
url: "system/checkerPostHandler.php",
method: "POST",
data: "checkSocks=CheckSocksNowPlease&ip=" + getIP + "&port=" + getPort,
success: function (getResponse) {
alert("PHP Response=>" + getResponse + " jQuery Response=>" + getIP);
if (socksList.length != 0) {
getNewSocks();
}
}
});
}
counter++;
}
您也可以使用
0
或socksList.forEach
。for of
示例:
for (const socks of socksList) {
var getTheSocksLine = $.trim(socksList[socks].replace(/\s+/g, ''));
//..other code
}
for of
示例:
socksList.forEach( socks => {
var getTheSocksLine = $.trim(socksList[socks].replace(/\s+/g, ''));
//..other code
});