SoFunction
Updated on 2025-03-10

Two functions that implement delayed or repeated execution of javascript

The following content is excerpted from "Detailed Explanation of the Development Technology of Conquering AJAX Web2.0". Today I think the lecture was very good in the library reading book, so I will excerpt it here! A small part of the content and code have been changed!

The window object provides two methods to implement the effect of the timer, namely () and. The former can make a piece of code run after a specified time; the latter can make a piece of code run once every time a specified time is passed. Their prototypes are as follows:
Copy the codeThe code is as follows:

(expression,milliseconds);  
(expression,milliseconds);  
Among them, expression can be a piece of code enclosed in quotes or a function name. At the specified time, the system will automatically call the function. When using the function name as the call handle, it cannot carry any parameters; and when using a string, the parameters to be passed can be written in it. The second parameter of the two methods is milliseconds, which indicates the number of milliseconds of delay or repeated execution. The following are two methods.
1. Method This method can execute a function in a delay, for example:

[Ctrl+A Select all Note:Introducing external Js requires refreshing the page before execution]

This code will cause the dialog "hello" to be displayed after the page is opened for 5 seconds. The last sentence can also be written as:
("hello()",1000); 
Readers can understand their differences, and there are such properties in the method.
If the delay execution is cancelled before the delay period reaches, the timeoutId method can be used, which receives an id, indicating a timer. This id is returned by the setTimeout method, for example:

[Ctrl+A Select all Note:Introducing external Js requires refreshing the page before execution]

In this way, if you want to cancel the display, just click any part of the page and execute the method so that the timeout operation is cancelled.

2. method
This method makes a function called every fixed time, which is a very common method. If you want to cancel the timed execution, it is similar to the clearTimeout method, you can call the method. The clearInterval method also receives the value returned by the setInterval method as a parameter. For example:

Copy the codeThe code is as follows:

//Define a repeatedly executed call
var id=("somefunction",10000);  
//Cancel timed execution
(id);  
The above code is only used to illustrate how to cancel a timed execution. In fact, in many occasions, the setInterval method is required. Here is a stopwatch to introduce the purpose of the setInterval function: the stopwatch will include two buttons and a text box for displaying time. The timing starts when the start button is clicked, the minimum unit is 0.01 seconds. Clicking the button again will stop the timing, and the text box displays the elapsed time. Another button is used to clear the current time. The implementation code is as follows:

[Ctrl+A Select all Note:Introducing external Js requires refreshing the page before execution]