setTimeout() in JavaScript is a very useful function that allows you to execute certain code after a certain amount of time. This method receives two parameters: the code to execute and the time in milliseconds. After the specified time, the executed code will be called.
The setTimeout() method is often used to create delay effects or perform specific operations after a specific time. You can execute any JavaScript code after a specified time, from simple alert windows and pop-up messages, to performing complex functions or operations.
Now, we will learn more about the use of setTimeout.
Delayed execution code
Delayed execution code is one of the most basic uses of setTimeout. The following is a basic example:
setTimeout(function(){ alert("Hello World!"); }, 3000);
The above code will pop up the "Hello World!" message box after the user waits for 3 seconds.
In the code, the first parameter is an anonymous function because we need to execute the code instead of calling the function directly. The second parameter specifies the time to delay, here it is milliseconds. In this example, we specify 3 seconds (3000 milliseconds) as the duration.
Manipulate DOM elements
setTimeout() can also be used to manipulate DOM elements. For example, the following code will change the content of the page title element after a certain period of time:
let pageTitle = document.getElementById("page-title"); setTimeout(function(){ pageTitle.textContent = "New Page Title"; }, 5000);
In this example, we first obtain a page element with the id "page-title" and store it in in a variable. We next used setTimeout() to change the textContent property of the element, changing the page title to "New Page Title". In this example, we used a delay of 5 seconds.
Cancel setTimeout()
In some cases, you may need to cancel the execution of the setTimeout() function before the timer has completed. In order to cancel the timer, you can use the clearTimeout() function. For example, the following code will create a timer in a call to the setTimeout() function as the first argument. After 5 seconds, if the user clicks on an element, the timer will be canceled.
let timer = setTimeout(function(){ alert("You've waited too long!"); }, 5000); document.getElementById("some-element").addEventListener("click", function(){ clearTimeout(timer); });
In this example, we first store the timer's ID number so that we can later cancel it using the clearTimeout() function. We next use the addEventListener() function to associate the code that clears the timer with the user clicking an element on the page. Using this approach, you can cancel a delayed operation when a certain condition is reached.
Conclusion
setTimeout() is a very useful function for executing code after a delay. It can be used for a variety of purposes, including displaying alert windows, manipulating DOM elements, adding delay effects, and performing complex functions. In all cases, you can use the clearTimeout() function to cancel the timer and prevent the execution of the operation.
The above is the detailed content of javascript settimeout usage. For more information, please follow other related articles on the PHP Chinese website!