What kinds of timers are there in javascript
This article introduces the relevant knowledge of "how many kinds of timers of javascript". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
There are two kinds of timers in javascript: 1, a single timer, which is defined by the setTimeout () method, which can execute a code block after a specified time (in milliseconds); and 2, a loop timer, which uses the setInterval () method definition to repeat some code according to the specified period (in milliseconds).
The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.
In JavaScript, we can use timers to delay the execution of some code or to repeat some code at regular intervals. For example, you can use a timer to regularly update advertisements on a page or display a real-time clock, and so on.
Two timers are provided in JavaScript: setTimeout () and setInterval ()
The method states that setTimeout () executes some code after the specified time (in milliseconds). The code will only execute setInterval () once to repeat the execution of some code in the specified period (milliseconds). The timer will not stop automatically unless the clearInterval () function is called to manually stop or close the browser window.
SetTimeout ()
The JS setTimeout () function is used to execute some code after a specified time, and the code is executed only once.
How to use it:
SetTimeout (code,millisec)
Code is required. The JavaScript code string to be executed after the function to be called.
Millisec is required. The number of milliseconds to wait before executing the code.
Example:
Get the verification code var time = 10; var num; var button = document.getElementById ("button"); var span = document.getElementById ("span") Function set () {num = setInterval ("click ()", 1000); button.disabled = true } function click () {if (time = = 0) {button.disabled = false; time = 10 Span [XSS _ clean] = ""; clearInterval (num) } else {Span [XSS _ clean] = time + "get the return value in seconds" time-- This is the end of the content of "what kinds of timers of javascript". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!