Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4554670
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T17:07:04+00:00 2026-05-21T17:07:04+00:00

I see there are lot’s of threads here in SO about asking for a

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T17:07:05+00:00Added an answer on May 21, 2026 at 5:07 pm

    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 ..

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I see there's a lot of talk here about how to go about sanitizing
Here is my form As you can see there is a lot of space
I can see there are a lot of questions for getting the number of
I see there are a lot of questions regarding threads modifying items in a
I see there are a lot of these questions, but I still can't figure
I can see that there was a lot of issues with this error message
I see there are lot's of examples in Ext JS where instead of actually
I am new to rails. What I see that there are a lot of
I see there is a question here but there is no definite answer. Has
I see there is version 1.5 and 3.0 beta , but I can't seem

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.