当需要类似 sleep 的功能时如何重构 javascript 代码?
我看到这里有很多关于请求 javascript sleep 函数的线程,我知道它只能使用 setTimeout
和 setInterval
来完成。
我使用 Greasemonkey 进行了一些用户脚本编写,并编写了一个脚本来加载大量页面并从中计算一些内容。它有效,但我不想太快地请求页面。
var html0=syncGet(url0); // custom function for sync ajax call.
// fill the something array
for(var i=0;i<something.length;i++)
{
// calculate url1,url2 using the array and the i variable
// do something with lots of local variables
var html1=syncGet(url1);
// I would put a sleep here.
// do something with the results
var html2=syncGet(url2);
// I would put a sleep here.
// do something with the results
// get url3 from the page loaded from url2
var html3=syncGet(url3);
// I would put a sleep here.
// do something with the results
}
// use the result of the for loop and lots of code will follow...
实际的代码比这更复杂和更长。
我为不存在的睡眠功能而哭泣(并理解为什么它不可能)如何重构它以使用 setTimeout、setInterval 函数并保持其可读(和工作)?
I see there are lot's of threads here in SO about asking for a javascript sleep function and I know it can be done only using setTimeout
and setInterval
.
I do some userscripting with greasemonkey and written a script that loads a lot of pages and calculates something from them. It works, but I don't want to request the pages too fast.
var html0=syncGet(url0); // custom function for sync ajax call.
// fill the something array
for(var i=0;i<something.length;i++)
{
// calculate url1,url2 using the array and the i variable
// do something with lots of local variables
var html1=syncGet(url1);
// I would put a sleep here.
// do something with the results
var html2=syncGet(url2);
// I would put a sleep here.
// do something with the results
// get url3 from the page loaded from url2
var html3=syncGet(url3);
// I would put a sleep here.
// do something with the results
}
// use the result of the for loop and lots of code will follow...
The actual code is a bit more complex and longer than this.
I'm crying for the nonexistent sleep function (and understand why is it not possible) How to refactor this to use setTimeout, setInterval functions and keep it readable (and working) too?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
例如这个:
For example this:
我遇到了类似的问题,在一些较旧的浏览器中,一个大循环阻塞了整个浏览器,我使用以下方法解决了它:
这里 initSuccessEnd 是一个回调函数,当一切完成时调用..
I had a similar problem where a big loop was blocking the whole browser in some older browsers, I solved it using :
here initSuccessEnd is a callback function called when all is finished ..
经过研究,我认为 Mozilla 的新迭代器生成器可能是最合适的。 (从FF2开始就支持)
我希望greatmonkey能处理这个问题......
After a research I think Mozilla's new iterator-generator stuff could be the most apropriate. (It's supported since FF2)
I hope greasemonkey will handle that...