Both settimeout () and SetInterval () in JavaScript refer to the delay in performing an operation.
However, setinterval () means that an action is performed at a specified time, and the operation is cycled continuously; SetTimeout () only executes once after the specified time has been deferred.
SetTimeout () can also achieve the effect of setinterval () in some cases, and a more classic example is calling itself inside a function. To the following:
function Example () {
...
SetTimeout ("Example ()", 1000);
}
Here is an example of a merry:
<!DOCTYPE HTML><HTML><Head><MetaCharSet= "Utf-8"><title> Merry</title><Script>varI= - -;functionSetColor () {varColor= NewArray ("Red","Blue","Green","Black","Turquoise","Coral","Orange","Lime","Violet"); varI=Math.Round (Math.random ()*8); document.getElementById ("text"). Style.color=color[i];}functionSetscroll () {vartext=document.getElementById ("text"); Text.style.marginLeft=I+ "px"; I=I+ Ten; if(i> $) {i= - -;} SetTimeout ("Setscroll ()", -);}functionChangeColor () {setinterval ("SetColor ()", -);}functionfunc () {ChangeColor (); Setscroll ();}</Script></Head><BodyOnLoad= "func ()"><Divstyle= "width:200px; overflow:hidden; Background-color: #CCC;"><DivID= "text"style= "width:100px;">Show Random Colors</Div></Div></Body></HTML>
SetTimeout () and SetInterval ()--Merry effect