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 ().