Difference between setTimeout () and setInterval () _ javascript skills

Source: Internet
Author: User
The timer setTimeout () and setInterval () are two different functions of the js timing function. The following is a brief introduction to the timer setTimeout () and setInterval () both functions are different from the timing functions of js.

SetTimeout ():

Explanation in the js Manual: used to call a function or computing expression after a specified number of milliseconds;

That is to say, execute the task after the set number of seconds.

Experiment code (change body background color ):

The Code is as follows:


SetTimeout (function (){
$ ("Body" ).css ("background", "red ");
},5000 );


SetInterval ():

Explanation in the js Manual: calling a function or computing expression according to the specified period (in milliseconds) will not stop calling the function until clearInterval () is called or the window is closed;

Execute your own effect code or function in the specified number of seconds.

Experiment code (several seconds ):

The Code is as follows:



Script
Var num = 0;
SetInterval (function () {$ (". clock" ).html (num ++)}, 1000 );
Script


Summary:

The setTimeout () method is to execute the function after a specified time and only execute the incoming handle function once.

The setInterval () method is to execute the incoming handle function once after each specified interval, and execute it cyclically until the window is closed or clearInterval ().

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.