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.
The operating environment of this tutorial: Windows 10 system, JavaScript version 1.8.5, Dell G3 computer.
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)
Execute the function after waiting for the specified number of milliseconds.
setInterval(function, milliseconds)
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);
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('Hello'); } </script> </body> </html>
Output result:
After 3 seconds of clicking the button:
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!