Simulate event delay, such as simulation results loading before displaying them on the page. This example uses recursive setTimeout()
to call a function that iterates over an array of pre-checked results to check JavaScript, Flash, browser versions, and more. I will write it into a jQuery plugin later when I have time, which is easy, just determine what options to provide to meet different usage situations .
Demo
setTimeout()
)// 数据和设置 var result = '预检查通过。', // 主结果的 HTML delay = 500, // 子结果的延迟 data = [ 'Javascript ', '系统 ', '设备 ', '浏览器 ', 'Flash ' ]; // 从数组索引 0 开始的自执行函数 (function process_els(el_index) { var el = data[el_index], precheckUl = $('#precheck ul'), loadingLi = $('<li class="loading">正在加载...</li>'), // 创建加载项 sysPreId = "syspre_" + el_index; // 显示加载图像 precheckUl.append(loadingLi.clone().attr("id", sysPreId)); // 模拟延迟后,将加载图像替换为子检查结果 setTimeout(function() { precheckUl.find('li.loading:first').replaceWith('<li>' + data[el_index] + '检查完成</li>'); // 添加检查完成状态 }, delay); // 模拟延迟,递归调用自身,直到所有数组元素都已处理 if (el_index + 1 < data.length) { setTimeout(function() { process_els(el_index + 1); }, delay); } else { setTimeout(function() { // 在所有子检查都插入后追加最终结果 precheckUl.after('<p>' + result + '</p>'); }, (delay * 2)); } })(0);
<div id="precheck"> <h2>系统检查</h2> <ul></ul> </div>
setTimeout
Analog Delay (FAQ)setTimeout
in JavaScript? setTimeout
is a method in JavaScript that allows you to schedule a function or specific block of code to run after delay. This is a way to delay the execution of a function. This is useful in various situations, such as creating animations, delaying alerts, or making a function wait for certain data.
setTimeout
How does it work? setTimeout
Works by taking two parameters: a function and a delay in milliseconds. When you call setTimeout
, it starts a timer. Once the timer reaches the specified delay, it executes the function. It should be noted that setTimeout
will not pause the execution of the remaining code. It just arranges the function to run later.
setTimeout
? Yes, you can use the clearTimeout
function to cancel setTimeout
. When you call setTimeout
, it returns a unique ID. You can pass this ID to clearTimeout
to stop the timer and prevent the function from being executed.
setTimeout
in jQuery? While jQuery has its own delay
method, you can still use setTimeout
in the jQuery context. You will use it like you would in plain JavaScript by passing functions and delays to setTimeout
.
setTimeout
and jQuery's delay
method? While both the setTimeout
and jQuery's delay
methods can be used to create delays, they work slightly differently. setTimeout
is used for JavaScript functions, while delay
is used for jQuery animation. delay
Specifically designed for use with jQuery's effect queues.
async/await
in setTimeout
? Yes, you can create delays in an asynchronous function using async/await
and setTimeout
. To do this, you need to wrap the setTimeout
in a promise.
setTimeout
not work? Your setTimeout
may not work for several reasons. You may have made a syntax error, or you may be trying to use a variable that has not been defined yet. The delay may also be too short to notice, or the function you are trying to execute may cause an error.
setTimeout
in a loop? Yes, you can use setTimeout
in a loop. However, since setTimeout
is non-blocking, all timeouts will be started simultaneously. If you want to create a series of delays, you need to add delays for each iteration of the loop.
setTimeout
? Testing functions using setTimeout
can be tricky because of the delay. One way is to use a test framework that supports asynchronous testing. Another way is to simulate setTimeout
so that you can control the execution time of the function.
setTimeout
? The maximum delay you can set using setTimeout
is 2147483647 milliseconds, about 24.8 days. If you try to set a longer delay than this, it will be reduced to 1.
The code has been modified, and loading prompts and completion status prompts have been added to make it clearer and easier to understand. The image path remains the same.
The above is the detailed content of Simulating Delay using jQuery and setTimeout(). For more information, please follow other related articles on the PHP Chinese website!