What is the difference between the two timers in javascript

青灯夜游
Release: 2021-12-07 15:20:03
Original
4337 people have browsed it

Difference: The setTimeout() timer executes certain codes after the specified time, and the code will automatically stop after executing it once; while the setInterval() timer executes certain codes repeatedly according to the specified period. The timer will not stop automatically and needs to be stopped manually by calling clearInterval().

What is the difference between the two timers in javascript

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

JavaScript timer, sometimes called "timer", is used to perform certain tasks after a specified time has passed, similar to the alarm clock in our lives.

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 ways to set timers, namely setTimeout() and setInterval().

setTimeout()Call a function or calculate an expression after the specified milliseconds

Pass in three parameters

  • code The JS code string to be executed after the function that must be called

  • millisec The number of milliseconds that must be waited before executing the code

  • lang Optional Generally do not write, select the script language type

setInterval()Call the function or calculate the expression according to the specified period. The method will continue to call the function until clearInterval is called or the window is closed

Pass in three parameters

  • code must be the function to be called or the code string to be executed

  • millisec Must be executed periodically or the time interval between calling code

  • lang Optional, leave half blank to select the language type

The difference between setTimeout() and setInterval() is as follows:

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.

[Related recommendations: javascript learning tutorial]

The above is the detailed content of What is the difference between the two 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template