"JavaScript" settimeout and SetInterval

Source: Internet
Author: User

SetTimeout is often mixed with setinterval, which is commonly used to call a function on a regular basis, such as a simple setinterval clock in "JavaScript" a dynamic time synchronized to local time (click the Open link). You can also write the following code:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

The effect is as follows:


But the actual meaning of settimeout, but not many people pay attention to,

In essence, the following function is executed after n milliseconds, such as the following code:

<script>settimeout ("Test ()"), Alert ("OK"), function test () {alert ("After 5 seconds ...");} </script>

Test () This function is scheduled to execute after 5 seconds, and then the program reads continuously, so the effect is as follows:


So settimeout can make the same effect as setinterval,

is because setinterval sets the XX function to execute every n milliseconds. So, to implement a time-specific effect, SetInterval is written out, and the page starts with a thread that executes the code every n milliseconds.

SetTimeout is written in the function, the first is called once, and then after n milliseconds after the set call itself, n milliseconds after the call itself, the task is completed. But you also have an n-millisecond call to your task. Therefore, in turn the cycle of endless, but also to achieve the effect of setinterval.

"JavaScript" settimeout and SetInterval

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.