1. Set Timer
Syntax: setinterval (function, time)
Attention:
function cannot be written with parentheses
SetInterval (fn,1000)//correct
SetInterval (FN (), 1000)//Error
|
The minimum time setting is not less than 14ms
2. Clear Timer
Syntax: Clearinterval (the name of the timer cleared)
3. Case study
JS part
Window.onload =function () {
vararr=[' red ', ' #FF9D0C ', ' #DBFF0D ', ' #3BFF0D ', ' #0DFFA2 ', ' #0DE8FF ', ' #0D52FF ', ' #210DFF ', ' #930DFF ', ' #FF0DB4 ']
Varstart = document.getElementById (' start ');
Varstop = document.getElementById (' Stop ');
Varprizedraw = document.getElementById (' Prizedraw ');
Start.onclick =function () {
Timer = setinterval (function () {
num = Math.floor (math.random () *100+1);
PrizeDraw.style.backgroundColor = Arr[i];
prizedraw.innerhtml = num;
Stop.onclick =function () {
|
CSS Section
#prizeDraw {
width:100px;
height:100px;
Background:rgba (255,68,253,0.5);
font-size:30px;
Text-align:center;
line-height:100px;
}
|
HTML section
<divid= "Prizedraw" ></div>
<inputtype= "button" value= "Starting" id= "Start" >
<inputtype= "button" value= "Pause" id= "Stop" >
|
Code Analysis:
Note that when running the timer to clear the timer first, otherwise, if the timer has been clicked, the timer will run multiple, stop also stop;
Clearinterval () can accept the cleared value of NULL;
An application that stores content in an array.
Original link: http://www.cnblogs.com/huyuzhu/p/6200840.html