Note that there are some explanatory texts on larger screens.

plurals
  1. POPause a for loop
    primarykey
    data
    text
    <p>Is it possible to pause a for loop in javascript/jquery?</p> <p>I have a <code>$.each</code>, that runs through a 63 long array, which have a <code>for</code> inside, which have another <code>for</code>, which have yet another <code>for</code> in it (Makes <code>each</code>><code>for</code>><code>for</code>><code>for</code>), now each of the <code>for</code> loops through the array, this makes 63^4 (Equals 15752961) different combinations, and that takes time...</p> <p>Sooo, is it possible to pause it 2sec, at 2k combinations?</p> <p>Reason why I want to pause the loop, is to unlock the UI...</p> <p>Code:</p> <pre><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 &lt; len; i++){ // 3969 for(var j = 0; j &lt; len; j++){ // 250047 for(var k = 0; k &lt; len; k++){ // 15752961 thing = val+i+j+k; l++; $("#loading").text(l+"/15752961"); console.log(l+"/15752961"); $("&lt;div&gt;"+thing+"&lt;/div&gt;&lt;br /&gt;").prependTo("body"); } } } }) }) </code></pre> <p>Ps. If someone think, they could clear up my question, please do so :D</p> <p><strong>/* EDIT */</strong> If I try to run this server side, I get a server error, both if I use <code>foreach</code> or <code>for</code></p> <pre><code>$smaa = range("a","z"); $store = range("A","Z"); $tal = range("0","9"); $arr = array_merge($smaa, $store, $tal); for($i = 0; $i &lt; $count; $i++){ // 62 for($j = 0; $j &lt; $count; $j++){ // 3844 for($k = 0; $k &lt; $count; $k++){ // 238328 for($l = 0; $l &lt; $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; } } } } </code></pre> <p>Please note, that I <strong>don't</strong> use both on the same time</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload