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 8030887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T00:54:49+00:00 2026-06-05T00:54:49+00:00

Is it possible to pause a for loop in javascript/jquery? I have a $.each

  • 0

Is it possible to pause a for loop in javascript/jquery?

I have a $.each, that runs through a 63 long array, which have a for inside, which have another for, which have yet another for in it (Makes each>for>for>for), now each of the for loops through the array, this makes 63^4 (Equals 15752961) different combinations, and that takes time…

Sooo, is it possible to pause it 2sec, at 2k combinations?

Reason why I want to pause the loop, is to unlock the UI…

Code:

var $arr = ["","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","0","1","2","3","4","5","6","7","8","9"],
len = $arr.length,
l = 0,
$("body").delay(1000).show(0, function(){
    $.each($arr, function(index, val){ // 63
        for(var i = 0; i < len; i++){ // 3969
            for(var j = 0; j < len; j++){ // 250047
                for(var k = 0; k < len; k++){ // 15752961
                    thing = val+i+j+k;
                    l++;
                    $("#loading").text(l+"/15752961");
                    console.log(l+"/15752961");
                    $("<div>"+thing+"</div><br />").prependTo("body");                                  
                }
            }
        }
    })
})

Ps. If someone think, they could clear up my question, please do so 😀

/* EDIT */
If I try to run this server side, I get a server error, both if I use foreach or for

$smaa = range("a","z");
$store = range("A","Z");
$tal = range("0","9");
$arr = array_merge($smaa, $store, $tal);

for($i = 0; $i < $count; $i++){ // 62
    for($j = 0; $j < $count; $j++){ // 3844
        for($k = 0; $k < $count; $k++){ // 238328
            for($l = 0; $l < $count; $l++){ // 14776336
                $finish[] = $arr[$i].$arr[$j].$arr[$k].$arr[$l];
            }
            $finish[] = $arr[$i].$arr[$j].$arr[$k];
        }
        $finish[] = $arr[$i].$arr[$j];
    }
$finish[] = $arr[$i];
}

foreach($arr as $first){ // 62
    $finish[] = $first;
    foreach($arr as $second){ // 3844
        $finish[] = $first.$second;
        foreach($arr as $third){ // 238328
            $finish[] = $first.$second.$third;
            foreach($arr as $fourth){ // 14776336
                $finish[] = $first.$second.$third.$fourth;
            }
        }
    }
}

Please note, that I don’t use both on the same time

  • 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-06-05T00:54:51+00:00Added an answer on June 5, 2026 at 12:54 am

    This is untested, but hopefully you will get the idea. Truthfully, this is something you probably should be handling on the server-side, or doing once and storing the output in the code.

    var $arr = ["","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","0","1","2","3","4","5","6","7","8","9"],
    len = $arr.length,
    l = 0,
    paused = false,
    writer = '',
    pauseAt = 2000;
    
    $("body").delay(1000).show(0, function(){
    
        function killForTwoSeconds(){ //function to timeout loop
             paused = true; //set to true to kill loop
             $('body').append(writer); //write to body only on pause (fewer writes to dom = faster script)
             setTimeout(function(){ // wait for 2 seconds
                  paused = false;
                  loopIt(); // re-run loop
             },2000);
        }
    
        function loopIt(){
            $.each($arr, function(index, val){ 
                for(var i = l; i < len; i++){ 
                    for(var j = l; j < len; j++){ //loops now start at l instead of 0 so when loop restarts it restarts at the right place
                        for(var k = l; k < len; k++){ 
                            if(l % pauseAt === 0){ // if l is divisible by 2000 kill 
                                killForTwoSeconds();
                            }
                            if(!paused){ //if not paused add item to writer
                                thing = val+i+j+k;
                                l++;
                                $("#loading").text(l+"/15752961");
                                console.log(l+"/15752961");
                                writer = "<div>"+thing+"</div><br />";                                  
                            }else if(index === len-1 && k === len-1){
                                $('body').append(writer); //final write
                            }else{
                                return false; // if paused return false
                            }
                        }
                    }
                }
            });
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following jQuery loop but inside each loop-action I have user interaction
Possible Duplicate: javascript: pause setTimeout(); Im using jQuery and working on a notification system
I have multiple CSV files which I need to parse in a loop to
Does anyone know or have good links that explain what iPhone's event loop does
Is it possible to have a command in common lisp which somehow temporarily pauses
I have a foreach loop that looks like this: foreach (var line in theCP4UnknownList.Distinct())
I'm wondering if its possible to pause a clustered index whenever bulk data is
tl;dr -- is this possible to pause or stop all running js on the
Possible duplicate question: Is there a way to indefinitely pause a thread? In my
How is it possible to parse command-line arguments that are to be interpreted as

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.