Differences between setTimeout and setinterval in JS

Source: Internet
Author: User

The answer to this question is too common. I copied a copy below and thanked the blogger.

Difference between setTimeout and setinterval in Rural Area Javascript

 

The only thing that deserves special attention is that JS is case sensitive. Do not write the function name wrong.

 

[SetTimeout]
SetTimeout (expression, latency)
During execution, the expression is executed once after the specified time delay after loading. Remember, the number of times is once.

 

The effect of the random number is displayed automatically when the setTimeout function is used:

<HTML>
<Head>
<SCRIPT>
Window. onload = sett;
Function sett ()
{
Document. Body. innerhtml = math. Random ();
SetTimeout ("sett ()", 500 );
}
</SCRIPT>
</Head>
<Body>
</Body>
</Html>

[Setinterval]
Setinterval (expression, interaction time)
It executes the expression once every specified time after loading.

The effect of the random number is displayed automatically when setinterval is used:

<HTML>
<Head>
<SCRIPT>
Function sett ()
{
Document. Body. innerhtml = math. Random ();
}
Setinterval ("sett ();", 500 );
</SCRIPT>
</Head>
<Body>
</Body>
</Html>

 

The two timer tools have their own advantages. When using setinterval, You need to manually stop tick trigger. The setTimeout nested in the method can be stopped based on the internal logic of the method.

 

In fact, the two things can be simulated with each other. The specific use depends on the needs at that time. Just as for can simulate all loops including branches, but also provides do and while.

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.