1. text box auto-increment (starting from zero) increments by 1 every second
<Input type = "text" name = "name" value = "0" id = "TXT"/>
<SCRIPT type = "text/JavaScript"> window. onload = function () {setinterval (function () {// obtain the text box object var OBJ = document. getelementbyid ('txt '); // obtain the value var txtvalue = parseint (obj. value); obj. value = txtvalue + 1 ;}, 1000) ;}</SCRIPT>
The timer setinterval returns a timer ID, which can be used to clear the timer.
Function F1 () {alert ('ga ');} // timer. The returned value is the ID var setid = Window of the timer. setinterval (F1, 2000); // The cleanup timer method. The parameter is the ID of a timer to clear the timer window. clearinterval (setid );
2, setTimeout (this timer is executed once)
// This timer is executed once var setid = setTimeout (function () {alert ('loaded ') ;}, 1000); cleartimeout (setid); // clear the timer
Example
<Input type = "button" name = "name" value = "Left" id = "btnleft"/> <input type = "button" name = "name" value =" right-hand: "id =" btnright "/>
<SCRIPT type = "text/JavaScript"> var def = 'left'; function F1 () {def = 'left';} function F2 () {def = 'right';} window. onload = function () {setinterval (function () {// obtain the title content var tit = document. title; If (DEF = 'left') {document. title = tit. substr (1) + tit. charat (0);} else if (DEF = 'right') {document. title = tit. charat (tit. length-1) + tit. substr (0, tit. length-1) ;}}, 1000); document. getelementbyid ('btnleft '). onclick = F1; document. getelementbyid ('btnright '). onclick = F2 ;}; </SCRIPT>
Setinterval and setTimeout timers