Home > Web Front-end > Front-end Q&A > What is the basic unit of timing in javascript

What is the basic unit of timing in javascript

WBOY
Release: 2022-02-21 17:03:03
Original
2061 people have browsed it

In JavaScript, the basic unit of timing is milliseconds; the timed event in JavaScript, that is, the object allows the execution of code at a specified time interval, and the specified time interval is measured in the specified number of milliseconds.

What is the basic unit of timing in javascript

The operating environment of this tutorial: Windows 10 system, JavaScript version 1.8.5, Dell G3 computer.

What is the basic unit of timing in JavaScript

JavaScript can be executed within a time interval.

This is the so-called timing event (Timing Events).

Timing Event

The window object allows the execution of code at specified intervals.

These time intervals are called timed events.

There are two key methods used through JavaScript:

setTimeout(function, milliseconds)
Copy after login

Execute the function after waiting for the specified number of milliseconds.

setInterval(function, milliseconds)
Copy after login

Equivalent to setTimeout(), but continues to execute the function repeatedly.

setTimeout() and setInterval() are both methods of the HTML DOM Window object.

Examples are as follows:

setTimeout() method

window.setTimeout(function, milliseconds);
Copy after login

window.setTimeout() method can be written without the window prefix.

The first parameter is the function to be executed.

The second parameter indicates the number of milliseconds before execution.

<html>
<body>
<p>点击“试一试”。等待 3 秒钟,页面将提示“Hello”。</p>
<button onclick="setTimeout(myFunction, 3000);">试一试</button>
<script>
function myFunction() {
  alert(&#39;Hello&#39;);
}
</script>
</body>
</html>
Copy after login

Output result:

What is the basic unit of timing in javascript

After 3 seconds of clicking the button:

What is the basic unit of timing in javascript

Related Recommended: javascript learning tutorial

The above is the detailed content of What is the basic unit of timing in javascript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template