JavaScript 等待 5 秒鐘(開發者的使用方法)
在 JavaScript 中,等待特定時間(例如 5 秒)是常見的需求。 無論你是想延遲某個操作還是模擬載入狀態,了解如何在 JavaScript 中實現延遲對於同步程式碼來說都至關重要。 在本文中,我們將探討在 JavaScript 中等待 5 秒鐘的各種方法,並為每種方法提供暫停 JavaScript 執行的範例。 此外,我們將藉助IronPDF for Node.js,使用非同步函數和設定超時函數來建立 PDF 檔案。
1. 使用 setTimeout()
setTimeout()函數是 JavaScript 內建函數,它會在指定的毫秒延遲後執行指定的函數或程式碼片段。
範例
console.log("Start");
// Schedules a function to be executed after 5000 milliseconds (5 seconds)
setTimeout(() => {
console.log("Waited for 5 seconds");
}, 5000);
console.log("End");console.log("Start");
// Schedules a function to be executed after 5000 milliseconds (5 seconds)
setTimeout(() => {
console.log("Waited for 5 seconds");
}, 5000);
console.log("End");在這個範例中, setTimeout()函數內部的程式碼將在延遲 5000 毫秒(或 5 秒)後執行。

2. 使用 Promise 和 async/await
您也可以將 Promises 與async/await結合使用,在 JavaScript 中建立延遲,也稱為非同步程式碼。
範例
async function delay() {
console.log("Start");
// Creates a promise that resolves after 5000 milliseconds (5 seconds)
await new Promise(resolve => setTimeout(resolve, 5000));
console.log("Waited for 5 seconds");
console.log("End");
}
// Call the async function
delay();async function delay() {
console.log("Start");
// Creates a promise that resolves after 5000 milliseconds (5 seconds)
await new Promise(resolve => setTimeout(resolve, 5000));
console.log("Waited for 5 seconds");
console.log("End");
}
// Call the async function
delay();在這個範例中, delay()函數使用async/await透過 Promise 暫停執行 5 秒鐘。

3. 使用 setInterval()
雖然setInterval()函數通常用於重複操作,但您也可以透過在所需時間後清除間隔來使用它來建立一次性延遲。
範例
console.log("Start");
let timer = setInterval(() => {
console.log("Waited for 5 seconds");
// Clear the interval after the desired delay
clearInterval(timer);
}, 5000);
console.log("End");console.log("Start");
let timer = setInterval(() => {
console.log("Waited for 5 seconds");
// Clear the interval after the desired delay
clearInterval(timer);
}, 5000);
console.log("End");在這裡, setInterval()函數每 5 秒重複執行提供的函數,直到我們使用clearInterval()函數清除間隔。

4. 使用 new Promise()
你可以使用new Promise()建立一個在指定延遲後解析的 Promise。
範例
console.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).then(() => {
console.log("Waited for 5 seconds");
console.log("End");
});console.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).then(() => {
console.log("Waited for 5 seconds");
console.log("End");
});在這個範例中, delay()函數傳回一個 Promise,該 Promise 會在 5 秒後解析,我們使用.then()在延遲後執行程式碼。
JavaScript 等待 5 秒(開發者如何操作):圖 4 - 使用 Promise 和 JavaScript delay() 和 then() 函數等待 5 秒的控制台輸出。
5. IronPDF JS簡介
IronPDF JavaScript Library for PDF Generation提供了一個 JavaScript 程式庫,使開發人員能夠直接從客戶端 JavaScript 操作和產生 PDF 文件。 它提供了一系列功能,可以使用 JavaScript 建立、編輯和轉換 PDF 文件。
安裝 IronPDF JS
要開始使用 IronPDF JS,您需要在專案中包含 IronPDF JavaScript 庫。 你可以透過 CDN 將其加入系統中,也可以直接從 IronPDF 網站下載。
npm install @ironsoftware/ironpdfnpm install @ironsoftware/ironpdf6. 使用 IronPDF 的 JavaScript 等待 5 秒
現在,讓我們看看如何將 JavaScript 程式碼延遲技術與 IronPDF 結合起來,使用非同步 JavaScript 解釋器在等待 5 秒鐘後創建 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.fromHtml(html);
// Save the PDF file
await pdfDocument.saveAs("Waited.pdf");
console.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.fromHtml(html);
// Save the PDF file
await pdfDocument.saveAs("Waited.pdf");
console.log("PDF Created after wait");
})();在這段程式碼片段中,非同步函數使用async/await和setTimeout()等待 5 秒鐘。 延遲之後,它使用 IronPDF 的PdfDocument.fromHtml()方法建立一個新的 PDF 文檔,其中包含簡單的 HTML 內容。 您可以根據自身需求取代 PDF 產生程式碼,或使用產生的 PDF 資料進行進一步處理。





