gpt4 book ai didi

javascript - 在 for 循环中设置动画延迟

转载 作者:行者123 更新时间:2023-12-03 05:36:27 24 4
gpt4 key购买 nike

我正在使用 jquery 动画函数来模拟 CPU 中处理的作业。我正在使用 id = jobj 动态创建 div,其中 j 是每次迭代的作业编号。之后,我将 div 元素分配给循环中每个进程的变量 $process = $(#'job' + j)。我想要的是作业以 1 秒的时间间隔一次进入队列。它们快速连续地进入队列,看起来好像只是基于动画的 3 个作业,所以有些事情发生了。

这就是我到目前为止所拥有的。

// 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

最佳答案

延迟内部循环刺激是不可能的,但您可以做这样的递归操作(根据需要更改刺激数量、延迟和起始值:

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

关于javascript - 在 for 循环中设置动画延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40711884/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com