Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use setTimeout and setInterval of JavaScript

Shulou Source: shulou.com Published: 2022-05-31 20:33:07 10月04日 Update

This article mainly explains "how to use setTimeout and setInterval of JavaScript". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use setTimeout and setInterval of JavaScript".

i. General description

First of all, the setTimeout () and setInterval () functions are two very important "time functions" in JavaScript. Therefore, in the process of learning JavaScript, we must have an in-depth study of these two functions, and their two functions are often used!

Among them, if the two functions are summarized in one sentence respectively, it should look like this:

SetTimeout (): this function can delay the execution of events according to the set time value.

SetInterval (): this function can periodically execute the time in accordance with the set time value as an interval.

Of course, it's just a brief description, and we'll explain it to you in the following part.

II. SetTimeout () function

The first is our setTimeout () function. What is its format?

SetTimeout (function,times)

Yes, it is such a simple format, where we need two parameters, a function and an int value, which represents the body of the function to be executed after a delay for a period of time. For example, let's look at this simple piece of code:

SetTimeout (function () {alert ("hello,setTimeout ()");}, 1000)

After running, it will pop up a pop-up box after a delay of 1 second, in which 1000 of the units are milliseconds!

So far, people think it's very easy, right? OK, I'll give you a piece of code, and you can guess what it will output:

For (var I = 1; I

Tags: Function countdown two code time learning variables that is run happy object format problem delay content actual queue loop output that is Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Shulou Technology Linux Shulou Information Redmi