2016-11-21 60 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