简体   繁体   English

有没有办法告诉网络浏览器继续“卡住”脚本?

[英]Is there a way to tell webbrowser to continiue script that is “stuck”?

I have created this recursive script that checks what address your on, and then checks in another file hierarchy if that folder your in are in, also exists on that place. 我创建了此递归脚本,该脚本检查您所在的地址,然后检查另一个文件层次结构(如果您所在的文件夹也位于该位置)。 Like for example say you're on somerandomsite.com/example/folder/folder1/folder1_1 and then you might want to redirect the user to somerandomsite.com/ another /example/folder/folder1/folder1_1 if that folder exists, otherwise just redirect him to somerandomsite.com/another (of course I have som special cases as well, like if folder/folder1/ exists but not folder/folder1/folder1_1, then redirect to somerandomsite.com/ another /example/folder/folder1/ etc. 例如,假设您在somerandomsite.com/example/folder/folder1/folder1_1上,然后您可能希望将用户重定向到somerandomsite.com/如果该文件夹存在,则将其重定向到另一个 / example / folder / folder1 / folder1_1,否则只需重定向他到somerandomsite.com/other(当然,我也有一些特殊情况,例如,如果folder / folder1 /存在但不存在folder / folder1 / folder1_1,则重定向到somerandomsite.com/ 另一个 / example / folder / folder1 /等。

Now to my problem, I have a real slow recursive implementation, and say that there are 50 folders in folder "example", 100 folders in folder, another folders in folder1, and last 100 folders the last level, then my implementation takes long time to "match" all the names. 现在,对于我的问题,我有一个真正的慢速递归实现,并说在“示例”文件夹中有50个文件夹,在文件夹中有100个文件夹,在folder1中有另一个文件夹,最后一级是最后100个文件夹,那么我的实现需要很长时间以“匹配”所有名称。

So some browsers display an error message that "some script has stopped working" since it is taking to long to execute. 因此,某些浏览器会显示一条错误消息,指出“某些脚本已停止工作”,因为执行该命令需要很长时间。 So my question is if there is some way to tell the browsers to let the script finish? 所以我的问题是,是否可以通过某种方式告诉浏览器让脚本完成?

You can find the code for the script here . 您可以在此处找到脚本的代码。

And for those who wounder how I perform the directory searches is that im creating xmlhttprequests to folders, and get a html version displaying all the folders, and then do a simple pattern match for each folder level. 对于那些讨厌如何执行目录搜索的人,我会创建对文件夹的xmlhttprequests,并获得一个显示所有文件夹的html版本,然后对每个文件夹级别进行简单的模式匹配。 In the example above I do 4 xmlhttpRequests, 在上面的示例中,我做了4个xmlhttpRequests,

One to somerandomsite.com, patternmatch for "example" 一个到somerandomsite.com, “ example”的模式匹配

One to somerandomsite.com/example/ patternmatch for "folder" 一个到somerandomsite.com/example/ patternmatch的“文件夹”

One to somerandomsite.com/example/folder/ patternmatch for "folder1" 一对一的“ folder1”到somerandomsite.com/example/folder/ patternmatch

One to somerandomsite.com/example/folder/folder1/ patternmatch for "folder1_1" 一个到somerandomsite.com/example/folder/folder1/模式匹配“ folder1_1”

No. That feature is designed explicitly for slow scripts, as you admitted yours is. 不,该功能是专门为慢速脚本而设计的,就像您承认的那样。 It's up to the user to decide whether to continue. 由用户决定是否继续。 If there was an escape hatch all sorts of harmful scripts would use it. 如果有一个逃生舱口,则各种有害脚本都将使用它。

I doubt that would occur with AJAX calls, since there is no actual script running while the request is made. 我怀疑AJAX调用会发生这种情况,因为发出请求时没有实际的脚本运行。 Are you positive you don't have any infinite loops in your code. 您是否确定您的代码中没有任何无限循环。

Oh, there is a problem with your code: 哦,您的代码有问题:

handleXML is your event handler for readystatechange , and it calls checkState which spawns a timeout to call itself every second. So every time the state changes you spawn another repeating checkState. handleXMLreadystatechange的事件处理程序,它调用checkState which spawns a timeout to call itself every second. So every time the state changes you spawn another repeating checkState. checkState which spawns a timeout to call itself every second. So every time the state changes you spawn another repeating checkState.

You don't need checkState to call itself, because handleXML already calls it and is called every time the state changes anyways. 您不需要checkState来调用自身,因为handleXML已经调用了它,并且每次状态改变时都会被调用。 Also if the status returned isn't 200 the checkState will call itself forever. 同样,如果返回的状态不是200,则checkState将永远调用自身。 I think browsers will be much happier with you if you change your checkState function to: 我认为,如果将checkState函数更改为:,浏览器会更快乐。

var checkState = function(xmlhttp, callback) {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        callback();
};

Good lord, do that on the server. 上帝,请在服务器上执行此操作。 You're already "getting httprequest lists of directories within a directory" - just do the whole thing on the server. 您已经在“获取目录内目录的httprequest列表” –只需在服务器上完成所有操作即可。

No. Look into using Web Workers to get the job done. 否。研究使用Web Workers完成工作。 But be wary of compatibility with older browsers. 但是请注意与旧版浏览器的兼容性。

The script is doing that because to much is happening at once. 脚本之所以这样做,是因为同时发生了很多事情。

Wait to start a new xmlhttprequest until the previous one has finished. 等待启动新的xmlhttprequest,直到上一个完成。

Other then using a faster browser, you can't force a script to keep running. 除了使用更快的浏览器之外,您不能强制脚本继续运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM