跳過到頁腳內容
NODE 說明

JavaScript 等待 5 秒鐘(開發者的使用方法)

在 JavaScript 中,等待特定的持續時間(如 5 秒)是一個常見的需求。 無論您是想延遲一項操作,還是模擬載入狀態,了解如何在 JavaScript 中實現延遲對於同步代碼至關重要。 在本文中,我們將探索[在 JavaScript 中等待 5 秒](https://byby您可以創建一個承諾(Promise)來在指定的延遲時間後解析,使用 new Promise()。dev/js-wait-n-seconds)的各種方法,並對每種方法的示例進行介紹以暫停 JavaScript 執行。 此外,我們將使用IronPDF for Node您可以創建一個承諾(Promise)來在指定的延遲時間後解析,使用 new Promise()。js和異步函數及設置超時函數來創建 PDF 文件。

1您可以創建一個承諾(Promise)來在指定的延遲時間後解析,使用 new Promise()。 使用 setTimeout()

setTimeout() 函數是一個內建的 JavaScript 函數,用於在指定的毫秒延遲後執行某個特定的函數或代碼段。

示例

console您可以創建一個承諾(Promise)來在指定的延遲時間後解析,使用 **new Promise()**。log("Start");

// Schedules a function to be executed after 5000 milliseconds (5 seconds)
setTimeout(() => {
    console您可以創建一個承諾(Promise)來在指定的延遲時間後解析,使用 **new Promise()**。log("Waited for 5 seconds");
}, 5000);

console您可以創建一個承諾(Promise)來在指定的延遲時間後解析,使用 **new Promise()**。log("End");
console您可以創建一個承諾(Promise)來在指定的延遲時間後解析,使用 **new Promise()**。log("Start");

// Schedules a function to be executed after 5000 milliseconds (5 seconds)
setTimeout(() => {
    console您可以創建一個承諾(Promise)來在指定的延遲時間後解析,使用 **new Promise()**。log("Waited for 5 seconds");
}, 5000);

console您可以創建一個承諾(Promise)來在指定的延遲時間後解析,使用 **new Promise()**。log("End");
JAVASCRIPT

在此示例中,setTimeout() 函數內的代碼將在 5000 毫秒(或 5 秒)延遲後執行。

![JavaScript 等待5秒(其在開發者中的運作方式):圖1 - 使用JavaScript setTimeout()函數在控制台輸出,並等待5000毫秒或5秒。## 2您可以創建一個承諾(Promise)來在指定的延遲時間後解析,使用 new Promise()。 使用承諾和異步/等待 ## 2您可以創建一個承諾(Promise)來在指定的延遲時間後解析,使用 new Promise()。 使用承諾和異步/等待

您還可以使用承諾與 async/await 來創建 JavaScript 中的延遲,也稱為異步代碼。

在此示例中,delay() 函數使用 async/await,通過一個承諾來暫停執行 5 秒。

示例

async function delay() {
    console您可以創建一個承諾(Promise)來在指定的延遲時間後解析,使用 **new Promise()**。log("Start");
    // Creates a promise that resolves after 5000 milliseconds (5 seconds)
    await new Promise(resolve => setTimeout(resolve, 5000));
    console您可以創建一個承諾(Promise)來在指定的延遲時間後解析,使用 **new Promise()**。log("Waited for 5 seconds");
    console您可以創建一個承諾(Promise)來在指定的延遲時間後解析,使用 **new Promise()**。log("End");
}

// Call the async function
delay();
async function delay() {
    console您可以創建一個承諾(Promise)來在指定的延遲時間後解析,使用 **new Promise()**。log("Start");
    // Creates a promise that resolves after 5000 milliseconds (5 seconds)
    await new Promise(resolve => setTimeout(resolve, 5000));
    console您可以創建一個承諾(Promise)來在指定的延遲時間後解析,使用 **new Promise()**。log("Waited for 5 seconds");
    console您可以創建一個承諾(Promise)來在指定的延遲時間後解析,使用 **new Promise()**。log("End");
}

// Call the async function
delay();
JAVASCRIPT

![JavaScript 等待5秒(其在開發者中的運作方式):圖2 - 使用JavaScript setTimeout()異步函數在Promise中統合輸出,並等待5000毫秒或5秒。雖然 setInterval() 函數通常用於重複執行操作,但您也可以用它來創建一次性延遲,方法是清除間隔設定後的期望時間。

3您可以創建一個承諾(Promise)來在指定的延遲時間後解析,使用 new Promise()。 使用 setInterval() 雖然 setInterval() 函數通常用於重複執行操作,但您也可以用它來創建一次性延遲,方法是清除間隔設定後的期望時間。

在此,setInterval() 函數每隔 5 秒重複提供的函數,直到我們用 clearInterval() 函數清除間隔。

![JavaScript 等待5秒(其在開發者中的運作方式):圖3 - 使用JavaScript setInterval()方法在控制台輸出,並等待5000毫秒或5秒。](/static-assets/pdf/blog/javascript-wait-5-seconds/javascript-wait-5-seconds-3您可以創建一個承諾(Promise)來在指定的延遲時間後解析,使用 new Promise()。webp)

示例

console您可以創建一個承諾(Promise)來在指定的延遲時間後解析,使用 **new Promise()**。log("Start");

let timer = setInterval(() => {
    console您可以創建一個承諾(Promise)來在指定的延遲時間後解析,使用 **new Promise()**。log("Waited for 5 seconds");
    // Clear the interval after the desired delay
    clearInterval(timer);
}, 5000);

console您可以創建一個承諾(Promise)來在指定的延遲時間後解析,使用 **new Promise()**。log("End");
console您可以創建一個承諾(Promise)來在指定的延遲時間後解析,使用 **new Promise()**。log("Start");

let timer = setInterval(() => {
    console您可以創建一個承諾(Promise)來在指定的延遲時間後解析,使用 **new Promise()**。log("Waited for 5 seconds");
    // Clear the interval after the desired delay
    clearInterval(timer);
}, 5000);

console您可以創建一個承諾(Promise)來在指定的延遲時間後解析,使用 **new Promise()**。log("End");
JAVASCRIPT

然後使用 clearInterval() 函數清除間隔。

4您可以創建一個承諾(Promise)來在指定的延遲時間後解析,使用 new Promise()。 使用 new Promise() 您可以創建一個承諾(Promise)來在指定的延遲時間後解析,使用 new Promise()。 Then clearing the interval using clearInterval() function您可以創建一個承諾(Promise)來在指定的延遲時間後解析,使用 new Promise()。](/static-assets/pdf/blog/javascript-wait-5-seconds/javascript-wait-5-seconds-3您可以創建一個承諾(Promise)來在指定的延遲時間後解析,使用 new Promise()。webp)

4您可以創建一個承諾(Promise)來在指定的延遲時間後解析,使用 new Promise()。 Using new Promise()

You can create a Promise that resolves after a specified delay using new Promise()您可以創建一個承諾(Promise)來在指定的延遲時間後解析,使用 new Promise()

示例

console您可以創建一個承諾(Promise)來在指定的延遲時間後解析,使用 **new Promise()**。log("Start");

// Delay function that returns a promise which resolves after `ms` milliseconds
const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));

// Use the delay function
delay(5000)您可以創建一個承諾(Promise)來在指定的延遲時間後解析,使用 **new Promise()**。then(() => {
    console您可以創建一個承諾(Promise)來在指定的延遲時間後解析,使用 **new Promise()**。log("Waited for 5 seconds");
    console您可以創建一個承諾(Promise)來在指定的延遲時間後解析,使用 **new Promise()**。log("End");
});
console您可以創建一個承諾(Promise)來在指定的延遲時間後解析,使用 **new Promise()**。log("Start");

// Delay function that returns a promise which resolves after `ms` milliseconds
const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));

// Use the delay function
delay(5000)您可以創建一個承諾(Promise)來在指定的延遲時間後解析,使用 **new Promise()**。then(() => {
    console您可以創建一個承諾(Promise)來在指定的延遲時間後解析,使用 **new Promise()**。log("Waited for 5 seconds");
    console您可以創建一個承諾(Promise)來在指定的延遲時間後解析,使用 **new Promise()**。log("End");
});
JAVASCRIPT

In this example, the delay() function returns a Promise that resolves after 5 seconds, and we use 您可以創建一個承諾(Promise)來在指定的延遲時間後解析,使用 new Promise()。then() to execute the code after the delay您可以創建一個承諾(Promise)來在指定的延遲時間後解析,使用 new Promise()

![JavaScript Wait 5 Seconds (How It Works For Developers): Figure 4 - Console output using a Promise and the JavaScript delay() and then() functions and waiting for 5 seconds您可以創建一個承諾(Promise)來在指定的延遲時間後解析,使用 new Promise()。](/static-assets/pdf/blog/javascript-wait-5-seconds/javascript-wait-5-seconds-4您可以創建一個承諾(Promise)來在指定的延遲時間後解析,使用 new Promise()。webp)

5您可以創建一個承諾(Promise)來在指定的延遲時間後解析,使用 new Promise()。 Intro to IronPDF JS

IronPDF JavaScript Library for PDF Generation provides a JavaScript library that enables developers to manipulate and generate PDF documents directly from client-side JavaScript您可以創建一個承諾(Promise)來在指定的延遲時間後解析,使用 new Promise()。 It offers a range of features to create, edit, and convert PDF files using JavaScript您可以創建一個承諾(Promise)來在指定的延遲時間後解析,使用 new Promise()

6. 結合JavaScript等候5秒與IronPDF使用

To start using IronPDF JS, you need to include the IronPDF JavaScript library in your project您可以創建一個承諾(Promise)來在指定的延遲時間後解析,使用 new Promise()。 You can include it via CDN or by downloading it directly from the IronPDF website您可以創建一個承諾(Promise)來在指定的延遲時間後解析,使用 new Promise()

npm install @ironsoftware/ironpdf
npm install @ironsoftware/ironpdf
SHELL

6您可以創建一個承諾(Promise)來在指定的延遲時間後解析,使用 new Promise()。 Using JavaScript Wait for 5 Seconds with IronPDF

Now, let's see how we can combine the JavaScript code delay techniques with IronPDF to create a PDF document after waiting for 5 seconds using an asynchronous JavaScript interpreter in the following code snippet您可以創建一個承諾(Promise)來在指定的延遲時間後解析,使用 new Promise()

您可以用您的特定需求替換PDF生成代碼,或者將生成的PDF數據用於進一步處理。

import { PdfDocument } from "@ironsoftware/ironpdf";

(async () => {
    const html = `<html><body><h1>Hello, IronPDF!</h1></body></html>`;
    // Wait for 5 seconds
    await new Promise(resolve => setTimeout(resolve, 5000));
    // Create PDF from the HTML content
    const pdfDocument = await PdfDocument您可以創建一個承諾(Promise)來在指定的延遲時間後解析,使用 **new Promise()**。fromHtml(html);
    // Save the PDF file
    await pdfDocument您可以創建一個承諾(Promise)來在指定的延遲時間後解析,使用 **new Promise()**。saveAs("Waited您可以創建一個承諾(Promise)來在指定的延遲時間後解析,使用 **new Promise()**。pdf");  
    console您可以創建一個承諾(Promise)來在指定的延遲時間後解析,使用 **new Promise()**。log("PDF Created after wait");
})();
import { PdfDocument } from "@ironsoftware/ironpdf";

(async () => {
    const html = `<html><body><h1>Hello, IronPDF!</h1></body></html>`;
    // Wait for 5 seconds
    await new Promise(resolve => setTimeout(resolve, 5000));
    // Create PDF from the HTML content
    const pdfDocument = await PdfDocument您可以創建一個承諾(Promise)來在指定的延遲時間後解析,使用 **new Promise()**。fromHtml(html);
    // Save the PDF file
    await pdfDocument您可以創建一個承諾(Promise)來在指定的延遲時間後解析,使用 **new Promise()**。saveAs("Waited您可以創建一個承諾(Promise)來在指定的延遲時間後解析,使用 **new Promise()**。pdf");  
    console您可以創建一個承諾(Promise)來在指定的延遲時間後解析,使用 **new Promise()**。log("PDF Created after wait");
})();
JAVASCRIPT

In this code snippet, the async function waits for 5 seconds using async/await and setTimeout()您可以創建一個承諾(Promise)來在指定的延遲時間後解析,使用 new Promise()。 After the delay, it creates a new PDF document using IronPDF's PdfDocument您可以創建一個承諾(Promise)來在指定的延遲時間後解析,使用 new Promise()。fromHtml() method with a simple HTML content您可以創建一個承諾(Promise)來在指定的延遲時間後解析,使用 new Promise()。 You can replace the PDF generation code with your specific requirements or use the generated PDF data for further processing您可以創建一個承諾(Promise)來在指定的延遲時間後解析,使用 new Promise()

![JavaScript Wait 5 Seconds (How It Works For Developers): Figure 5 - Console output using the JavaScript setTimeout() function along and waiting for 5 seconds您可以創建一個承諾(Promise)來在指定的延遲時間後解析,使用 new Promise()。 Then the IronPDF code runs to convert the HTML string to PDF document and displays the message "PDF Created after wait" in the console您可以創建一個承諾(Promise)來在指定的延遲時間後解析,使用 new Promise()。](/static-assets/pdf/blog/javascript-wait-5-seconds/javascript-wait-5-seconds-5您可以創建一個承諾(Promise)來在指定的延遲時間後解析,使用 new Promise()。webp)

結論

Waiting for a specific duration in JavaScript is a common task that developers often encounter您可以創建一個承諾(Promise)來在指定的延遲時間後解析,使用 new Promise()。 In this article, we explored various methods to wait for 5 seconds in JavaScript, including using setTimeout(), Promises with async/await, setInterval(), and new Promise() and JavaScript sleep function您可以創建一個承諾(Promise)來在指定的延遲時間後解析,使用 new Promise()

Additionally, we introduced IronPDF JS for managing PDF files using JavaScript您可以創建一個承諾(Promise)來在指定的延遲時間後解析,使用 new Promise()。 For more code examples, visit the IronPDF Node您可以創建一個承諾(Promise)來在指定的延遲時間後解析,使用 new Promise()。js Examples您可以創建一個承諾(Promise)來在指定的延遲時間後解析,使用 new Promise()

By understanding these techniques and tools, you can effectively implement delays in your JavaScript applications and utilize them in more complex tasks, such as generating PDF documents or performing asynchronous operations您可以創建一個承諾(Promise)來在指定的延遲時間後解析,使用 new Promise()。 Whether you are a beginner or an experienced developer, having a solid grasp of these fundamentals will enhance your coding skills and enable you to write more efficient and robust JavaScript applications您可以創建一個承諾(Promise)來在指定的延遲時間後解析,使用 new Promise()

Darrius Serrant
全棧軟件工程師 (WebOps)

Darrius Serrant 擁有邁阿密大學計算機科學學士學位,目前任職於 Iron Software 的全栈 WebOps 市場營銷工程師。從小就迷上編碼,他認為計算既神秘又可接近,是創意和解決問題的完美媒介。

在 Iron Software,Darrius 喜歡創造新事物,並簡化複雜概念以便於理解。作為我們的駐場開發者之一,他也自願教學生,分享他的專業知識給下一代。

對 Darrius 來說,工作令人滿意因為它被重視且有實際影響。