Setinterval and setTimeout

Source: Internet
Author: User
<SCRIPT type = "text/JavaScript"> $ (function () {function setwidth () {$ ("# targetcon" ).css ({width: $ ("# divcon "). width ()-50 + "PX"}) ;}setwidth (); setinterval (setwidth, 50) ;}) </SCRIPT>
To know how they stop, first we need to understand their operating mechanisms and principles:

To learn more about setinterval:

Bytes --------------------------------------------------------------------------------------------------------

1. html Dom setinterval () method


Definition and usage
The setinterval () method can call functions or computation expressions according to the specified period (in milliseconds.

The setinterval () method does not stop calling the function until clearinterval () is called or the window is closed. The id value returned by setinterval () can be used as a parameter of the clearinterval () method.

Syntax
Setinterval (Code, millisec [, "Lang"])

Parameter description
Code required. The function to be called or Code String.
Millisec is required. The interval between periodical execution or call codes, in milliseconds.


Return Value
A value that can be passed to window. clearinterval () to cancel periodic code execution.

---------------------------------------------------

2. html Dom clearinterval () method

Definition and usage
The clearinterval () method can cancel the timeout set by setinterval.

The parameter of the clearinterval () method must be the id value returned by setinterval.

Syntax
Clearinterval (id_of_setinterval)

Parameter description
Id_of_setinterval is the ID value returned by setinterval.


How to stop:

The following example calls the clock () function every 50 milliseconds. You can also use a button to stop the clock:

<HTML>
<Body>

<Input type = "text" id = "Clock" size = "35"/>
<Script language = JavaScript>
VaR Int = Self. setinterval ("clock ()", 50)
Function Clock ()
{
VaR T = New Date ()
Document. getelementbyid ("Clock"). value = T
}
</SCRIPT>
</Form>
<Button onclick = "Int = Window. clearinterval (INT)">
Stop interval </button>

</Body>
</Html>

Bytes --------------------------------------------------------------------------------------------------------

To learn more about setTimeout:

1. html Dom setTimeout () method

Definition and usage
The setTimeout () method is used to call a function or computing expression after a specified number of milliseconds.

Syntax
SetTimeout (Code, millisec)

Parameter description
Code required. The javascript code string to be executed after the function to be called.
Millisec required. The number of milliseconds to wait before executing the code.


Tips and comments
Note: setTimeout () is executed only once. If you want to call it multiple times, use setinterval () or let the Code itself call setTimeout () again ().

Instance. In this example, a prompt box will pop up five seconds after you click the button:
<HTML>
<Head>
<SCRIPT type = "text/JavaScript">
Function Timedmsg ()
{
VaR T = setTimeout ("alert ('5 seconds! ') ", 5000)
}
</SCRIPT>
</Head>

<Body>
<Form>
<Input type = "button" value = "display timed alertbox! "
Onclick = "timedmsg ()">
</Form>
<P> click on the button above. An alert box will be
Displayed after 5 seconds. </P>
</Body>

</Html>
-----------------------------
2. html Dom cleartimeout () method
You can define and use the cleartimeout () method to cancel the timeout set by the setTimeout () method. Syntax cleartimeout (id_of_settimeout)



Parameter description
Id_of_setinterval is the ID value returned by setTimeout. This value identifies the delayed execution code block to be canceled.



The following example calls the timedcount () function every second. You can also use a button to terminate the scheduled message: <HTML>
<Head>
<SCRIPT type = "text/JavaScript">
VaR C = 0
VaR T
Function Timedcount ()
{
Document. getelementbyid ('txt '). value = C
C = C + 1
T = setTimeout ("timedcount ()", 1000)
}
Function Stopcount ()
{
Cleartimeout (t)
}
</SCRIPT>
</Head>
<Body>

<Form>
<Input type = "button" value = "Start count! "Onclick =" timedcount () ">
<Input type = "text" id = "TXT">
<Input type = "button" value = "Stop count! "Onclick =" stopcount () ">
</Form>

</Body>
</Html>



Tag: setinterval setTimeout

 

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.