Setinterval and setTimeout timers

Source: Internet
Author: User

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

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.