2016-11-21 19 views
0

jqueryアニメーション機能を使用して、CPUで処理されているジョブをシミュレートしています。私はdivsid = jobjで動的に作成し、jは各繰り返しでジョブ番号にします。その後、div要素をループ内の各プロセスの変数$process = $(#'job' + j)に割り当てます。私が望むのは、ジョブが1秒間隔で1つずつキューに入ることです。すばやく連続して待ち行列に入っていますが、それはあたかもアニメーションに基づいた3つのジョブのように思えます。アニメーションのforループ内で遅延を設定する

これはこれまで私が行ってきたことです。

// Wait queue 
// While jobs are using CPU, other jobs are coming in 
for (j = i; j < json_process.length; j++){ 

    // Check if CPU is being used 
    // Processes will be added to the input queue 
    // First we check if the queue is empty 
    if (json_process[j].waitTime != 0){ 

     // Convert strings to numbers in the array for calculation and comparison 
     completedCPUTime = parseFloat(json_process[i].completedCPUTime); // update CPU time 
     joiningInputQueueTime = parseFloat(json_process[j].joiningInputQueueTime); // update joining queue time 

     // Send animation[i] to input queue if it has to wait: 
     if(completedCPUTime > joiningInputQueueTime && waitIndex < j){ 

      // Create job Div element 
      elm = '<div id="job' + j + '" ' + 
        'style="background-color:red; width:5px; height:50px; position:absolute; margin-top:30px;" >' + 
        ' </div>'; 
      $(elm).appendTo('#animate'); 


       // Get process div 
       var $process = $('#job' + j); 
       var pos = process.offset().left; // position of process 
       // The end of the queue 
       var queueEnd = queue.offset().left + queue.outerWidth() - process.outerWidth(); 


      input_queue.push(j); // push job in input queue 
      alljobs.push(j); 


        // Animate Div element $process 
      // Pausing loop on each job going into queue 
      setTimeout(function() { 
       // Send Job joiningInputQueueTime[j] to queue div 
       $process.stop().animate(
        {left: queueEnd}, 
        {duration: 1000}); 
      }, 5000); 

      //This will keep track of the last index waiting 
      waitIndexCurrent = j; 

     }// End of animation condition 
    }// End of wait condition 
} // End of wait queue time For Loop 
+0

あなたの記事を更新して作成することができますコードスニペットまたはjsfiddleを使用して、実行/テストできる最新のコードを表示しますか? –

+0

OKは – Jam1

+0

を実行するか、これらのGoogle検索結果を最初に読み、テストしてみることができますか?https://www.google.com.ph/# q = javascript + delay –

答えて

1

ループの炎症の内側に延期は不可能ですが、あなたが望むように、このような再帰的なもの(炎症の変更番号、遅延及び開始値を行うことができます。

(function theLoop (j) { 
     setTimeout(function() { 
      //your code here 
      if (j++ < number of irritations) {   
      theLoop(j);  
     } 
     }, delay in miliseconds); 
})(starting value); 
+0

こんにちは、私はなぜそれがイムであるか説明できますか可能? – Jam1

+0

@ Jam1遅くて申し訳ありません。なぜなら、for、inのプログラムヒット遅延が(それが何かが遅れていることを認識できないので)それを待っていないからです。同じ状況の中で、遅れて内部から関数を呼び出そうとすると。それは関数を呼び出す、それは遅延を参照していないと関数呼び出しを積み重ねている。 forが終了し、おそらく遅延が渡されたときと同じ時間に前に呼び出されたすべての関数が残ります。それは不可能です。しばらくの間、同じことをする。 – someRandomSerbianGuy

関連する問題