What are the timers in javascript

青灯夜游
Release: 2021-11-26 18:04:27
Original
4323 people have browsed it

There are two types of timers in JavaScript: 1. Single timer, defined using the setTimeout() method, which can execute a code block once after the specified time (in milliseconds); 2. Loop timer, Use the setInterval() method to define certain codes to be executed repeatedly according to a specified period (in milliseconds).

What are the timers in javascript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

In JavaScript, we can use timers to delay the execution of certain codes, or to repeatedly execute certain codes at fixed intervals. For example, you can use a timer to regularly update the ads on the page or display a real-time clock, etc.

JavaScript provides two timers: setTimeout() and setInterval()

Method Description
setTimeout() After the specified time (unit is milliseconds), execute certain codes. The code will only be executed once
setInterval () Repeatedly execute certain codes according to the specified period (unit: milliseconds). The timer will not stop automatically unless the clearInterval() function is called to manually stop or close the browser window

setTimeout()

JS setTimeout() function is used to execute certain code after the specified time. The code is only executed once.

Usage:

setTimeout(code,millisec)
Copy after login
  • code Required. The string of JavaScript code to be executed after the function to be called.

  • #millisec Required. The number of milliseconds to wait before executing code.

Example:

   
Copy after login

setInterval()

JS setInterval() function can define a timer that can be executed repeatedly , each execution needs to wait for the specified time interval.

Usage:

setInterval(code,millisec[,"lang"])
Copy after login
  • code Required. A function to be called or a string of code to be executed.

  • #millisec Required. The time interval, in milliseconds, between periodic executions or calls to code.

Example:

   
Copy after login

[Recommended learning:javascript advanced tutorial]

The above is the detailed content of What are the timers 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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!