2016-11-21 64 views
0

我正在使用jquery動畫函數來模擬CPU中正在處理的作業。我使用id = jobj動態創建divs,其中j是每次迭代的作業編號。之後,我將div元素分配給循環中每個進程的變量$process = $(#'job' + j)。我想要的是工作在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

好的將會這樣做 – Jam1

+0

或者您可以嘗試先閱讀並測試這些谷歌結果?https://www.google.com.ph/# q = javascript +延遲 –

回答

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對不起,以便延遲迴答。這是不可能的,因爲當程序遇到延遲時,因爲沒有等待它(因爲它不能識別某個東西被延遲了)。如果你試圖從裏面調用函數,那麼延遲就是這種情況。它調用函數,它看不到延遲,它是堆棧函數調用。比什麼時候完成和延遲可能通過,它會在同一時間之前調用所有函數的其餘部分。所以這是不可能的。同時做和做同樣的事情。 – someRandomSerbianGuy