当需要类似 sleep 的功能时如何重构 javascript 代码?

发布于 2024-11-04 05:20:03 字数 959 浏览 2 评论 0原文

我看到这里有很多关于请求 javascript sleep 函数的线程,我知道它只能使用 setTimeoutsetInterval 来完成。

我使用 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 技术交流群。

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

发布评论

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

评论(3

俏︾媚 2024-11-11 05:20:03

例如这个:

var urls = ["your","u","r","l´s"];
var htmls = new Array(urls.length);
var time = 1000;
for(var i=0;i<urls.length;i++){
    (function(i){
        setTimeout(function(){
            htmls[i] = syncGet(urls[i]);
            if(i == urls.length-1){
                //continue here
            }
        },time*i);
    })(i);
}

For example this:

var urls = ["your","u","r","l´s"];
var htmls = new Array(urls.length);
var time = 1000;
for(var i=0;i<urls.length;i++){
    (function(i){
        setTimeout(function(){
            htmls[i] = syncGet(urls[i]);
            if(i == urls.length-1){
                //continue here
            }
        },time*i);
    })(i);
}
¢好甜 2024-11-11 05:20:03

我遇到了类似的问题,在一些较旧的浏览器中,一个大循环阻塞了整个浏览器,我使用以下方法解决了它:

function handlenext(idx,length) {
    idx++

    //do your stuff here base on idx.


    if (idx < length) {
        setTimeout(function(){handlenext(idx,length)},1)
    } else {
        initSuccessEnd()
    }
}

var ln = something.length;


if (ln>0) {
    handlenext(0,ln);
} else {
    initSuccessEnd()
}

这里 initSuccessEnd 是一个回调函数,当一切完成时调用..

I had a similar problem where a big loop was blocking the whole browser in some older browsers, I solved it using :

function handlenext(idx,length) {
    idx++

    //do your stuff here base on idx.


    if (idx < length) {
        setTimeout(function(){handlenext(idx,length)},1)
    } else {
        initSuccessEnd()
    }
}

var ln = something.length;


if (ln>0) {
    handlenext(0,ln);
} else {
    initSuccessEnd()
}

here initSuccessEnd is a callback function called when all is finished ..

随风而去 2024-11-11 05:20:03

经过研究,我认为 Mozilla 的新迭代器生成器可能是最合适的。 (从FF2开始就支持)

function doSomething()
{
   //.....
      var html=syncGet(url1);
      yield true;
      var html2=syncGet(url2);
      yield true;
      var html3=syncGet(url3);
      yield true;
   //......
   yield false;
}

function iteratorRunner(iterator,timeout)
{
    if (iterator.next()) 
    {
       setTimeout(function(){iteratorRunner(iterator,timeout)},timeout);
    }
    else
    {
       iterator.close();
    }
}

var iterator=doSomething(); // returns an iterator immediately
iteratorRunner(iterator,1000); // runs the iterator and sleeps 1 second on every yield.

我希望greatmonkey能处理这个问题......

After a research I think Mozilla's new iterator-generator stuff could be the most apropriate. (It's supported since FF2)

function doSomething()
{
   //.....
      var html=syncGet(url1);
      yield true;
      var html2=syncGet(url2);
      yield true;
      var html3=syncGet(url3);
      yield true;
   //......
   yield false;
}

function iteratorRunner(iterator,timeout)
{
    if (iterator.next()) 
    {
       setTimeout(function(){iteratorRunner(iterator,timeout)},timeout);
    }
    else
    {
       iterator.close();
    }
}

var iterator=doSomething(); // returns an iterator immediately
iteratorRunner(iterator,1000); // runs the iterator and sleeps 1 second on every yield.

I hope greasemonkey will handle that...

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