Javascript settimeout 递归函数?

发布于 2024-12-16 16:18:02 字数 607 浏览 0 评论 0原文

function theFunction(a){
    var newNum = a+1;
    var iframe = document.createElement("iframe");
    var currURL = urlArray[a];
    iframe.src = currURL;
    iframe.frameBorder = "0";
    iframe.height = "0";
    iframe.width = "0";

iframe.onload = function(){
    document.getElementById('number').innerHTML = a;
};
    var arrayLength = someArray.length;

    if(a != arrayLength){
     theFunction(newNum);
     }
}

这是我通过数组递归的基本函数。不管怎样,有时当它经历时(在//不做任何事情的部分)。它会卡住,我希望它在 2 秒后超时并移至下一个。这可以做到吗,还是我必须完全停止该功能?

编辑:设置超时可能不是我想要的,而是某种方法来停止该功能并移至下一个。

编辑2:在代码中添加更多内容

function theFunction(a){
    var newNum = a+1;
    var iframe = document.createElement("iframe");
    var currURL = urlArray[a];
    iframe.src = currURL;
    iframe.frameBorder = "0";
    iframe.height = "0";
    iframe.width = "0";

iframe.onload = function(){
    document.getElementById('number').innerHTML = a;
};
    var arrayLength = someArray.length;

    if(a != arrayLength){
     theFunction(newNum);
     }
}

This is my basic function that is recursive through an array. Anyways, sometimes as it goes through (during the //do nothing part). It will get stuck, and I want it to timeout after say 2 seconds AND move onto the next one. Is this possible to do, or will I just have to completely stop the function?

EDIT: Set timeout might not be what I am looking for, but some way to just stop the function and move to the next one.

EDIT 2: Added more to the code

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

老子叫无熙 2024-12-23 16:18:02

嗯。您确切知道当它“卡住”时会发生什么吗?因为如果“卡住”意味着遇到循环或其他错误,那么 setTimeout 将无济于事。根据我的经验,当给定的延迟过去或其他代码停止执行时(以最后发生的为准),使用 setTimeout 设置执行的代码将会发生。换句话说,我认为您不能使用 setTimeout 中断当前正在运行的代码,这似乎就是您所要求的。

我知道这有点没有答案;我建议改为确定卡住的本质。

Hmmm. Do you know exactly what's happening when it get "stuck"? Because if "stuck" means hitting a loop or some other error then setTimeout won't help you. In my experience, code set to execute with setTimeout will happen when the given delay has passed or when other code has stopped executing, whichever happens LAST. In other words, I don't think you can interrupt currently running code with a setTimeout, which seems to be what you're asking.

I know that's sort of a non-answer; I suggest nailing down the nature of the stuckness instead.

挥剑断情 2024-12-23 16:18:02

您可以“尝试”使用 iframe.contentDocument.readyState,但我说“尝试”时信心不足,因为通过我的测试,它总是会达到“完整”状态,但可能当您运行到某些页面时需要一段时间才能完成负载他们可能会停留在之前的一些状态。

有关此以及不同状态的详细信息,请查看此

http://msdn.microsoft.com/en-us/library/ms534358%28v=vs.85%29.aspx

You could "try" to use iframe.contentDocument.readyState, but I say "try" with little confidence because with my tests it always came out to the "complete" state, but possibly when you run in to some pages that take awhile to load they may stay in some of the previous states.

For more information on this and the different states view this

http://msdn.microsoft.com/en-us/library/ms534358%28v=vs.85%29.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文