Javascript $。是否使用不同的URL在循环中获取请求?
我想检查一个文本文件,如果它包含该文本,然后转到下一个get请求并检查另一个文本文件,以查看第二个文本文件是否包含某些文本 我试着将其放入一个while循环,如下所示:Javascript $。是否使用不同的URL在循环中获取请求?,javascript,jquery,ajax,Javascript,Jquery,Ajax,我想检查一个文本文件,如果它包含该文本,然后转到下一个get请求并检查另一个文本文件,以查看第二个文本文件是否包含某些文本 我试着将其放入一个while循环,如下所示: function testMessage() { const filePaths = [ "http://example.com/trigger.txt", "http://example.com/userexceptions.txt
function testMessage()
{
const filePaths =
[
"http://example.com/trigger.txt",
"http://example.com/userexceptions.txt",
"http://example.com/message.txt"
];
const trigger = "true";
var currentStage = 0;
const finalStage = 2;
var breakLoop = false;
while (currentStage < finalStage)
{
$.get(filePaths[currentStage], function(data)
{
if (currentStage == 0)
{
const compareText = data.localeCompare(trigger, "en", {sensitivity: "base"});
if (compareText == 0) //if the first text file contains the trigger "true", continue
{
someGlobalVariable = true;
currentStage++;
} else {
breakLoop = true;
}
} else if (currentStage == 1) //if the second text file contains the username of the current user, break the loop
{
const rawUsers = data;
const userExceptions = rawUsers.split(';');
if (userExceptions.indexOf(currentUser) > -1)
{
console.log("User exception is: " + userExceptions[userExceptions.indexOf(currentUser)]);
breakLoop = true;
} else {
currentStage++;
}
} else if (currentStage == 2)
{
globalNotification = data;
notification.global(globalNotification);
console.log("Global notification displayed.");
} else {
console.log("We're here now.");
}
}, 'text')
.fail(function()
{
console.log("Global notifications failed at stage: " + currentStage);
});
if (breakLoop)
break;
}
}
函数testMessage()
{
常量文件路径=
[
"http://example.com/trigger.txt",
"http://example.com/userexceptions.txt",
"http://example.com/message.txt"
];
const trigger=“true”;
var-currentStage=0;
const finalStage=2;
var breakLoop=false;
while(当前阶段<最终阶段)
{
$.get(文件路径[currentStage],函数(数据)
{
如果(currentStage==0)
{
const compareText=data.localeCompare(触发器,“en”{sensitivity:“base”});
if(compareText==0)//如果第一个文本文件包含触发器“true”,是否继续
{
someGlobalVariable=true;
currentStage++;
}否则{
breakLoop=true;
}
}else if(currentStage==1)//如果第二个文本文件包含当前用户的用户名,则中断循环
{
const rawUsers=数据;
const userExceptions=rawUsers.split(“;”);
if(userExceptions.indexOf(currentUser)>-1)
{
log(“用户异常为:”+userExceptions[userExceptions.indexOf(currentUser)]);
breakLoop=true;
}否则{
currentStage++;
}
}else if(currentStage==2)
{
全局通知=数据;
通知.全球(全球通知);
log(“显示全局通知”);
}否则{
log(“我们现在在这里。”);
}
}(“文本”)
.fail(函数()
{
log(“阶段:+currentStage的全局通知失败”);
});
if(断开循环)
打破
}
}
我还尝试使用一个switch语句而不是多个if-else语句,并导致页面中断,因此我认为它没有正确地中断switch和循环
当我运行这个时,循环似乎永远不会结束,所以它会中断页面?我将以一种非常不同的方式实现它。我想你应该读一下
fetch
和promissions
()。这将很容易解决此类问题
无论如何。。。我试着对你的代码进行最小程度的修改。这是如何做到的:
- 删除控制变量,因为可以使用数组长度和测试结果
- 使用数组长度,您可以简单地向数组中添加更多项,这样就可以了
- 由于每个文件都需要不同的测试,我将字符串URL数组更改为对象数组,在其中我们可以为每个文件添加
方法test
var被breakLoop
方法返回替换test
测试被递归测试取代breakLoop
- while循环被异步递归
方法取代。这使得getFiles
也是异步的,所以我给它添加了一个testMessage
参数callback
在其对索引0的声明之后被调用。 **它使用jQuery的ajax,并根据结果运行当前阶段测试。 **当返回true且这不是最后一项时,它将调用下一阶段的getFiles
。 **所以。。。这意味着它不是平行的。我相信这是你需要的getFiles
函数测试消息(回调)
{
const trigger=“true”;
常量文件=
[
{
url:“http://example.com/trigger.txt",
测试(数据){
const compareText=data.localeCompare(触发器,“en”{sensitivity:“base”});
if(compareText==0)//如果第一个文本文件包含触发器“true”,是否继续
{
someGlobalVariable=true;
返回true;
}
否则返回false;
}
},
{
url:“http://example.com/userexceptions.txt",
测试(数据){
const rawUsers=数据;
const userExceptions=rawUsers.split(“;”);
if(userExceptions.indexOf(currentUser)>-1)
{
log(“用户异常为:”+userExceptions[userExceptions.indexOf(currentUser)]);
返回false;
}
返回true;
}
},
{
url:“http://example.com/message.txt",
测试(数据){
全局通知=数据;
通知.全球(全球通知);
log(“显示全局通知”);
返回true;
}
}
];
(函数getFiles(currentStage)
{
$.get(文件[currentStage].url,函数(数据)
{
if(文件[currentStage].test(数据))
{
如果(files.length>currentStage+1)获取文件(currentStage+1)
else回调(null,“Success!”)
}
else回调(错误(`Stage${currentStage}的测试失败。`,null))
}(“文本”)
.fail(函数()
{