SetTimeout, cleartimeout, setinterval, clearinteral

Source: Internet
Author: User

Set the timer and run the Specified Code after a period of time. The difference between setTimeout and setinterval is that the Code specified by the setTimeout function is only executed once.

Method 1:

Window. setTimeout ("alert ('OK')", 5000 );

Method 2:

Window. setTimeout (function ()
{
Alert ("OK ");
},5000 );

Method 3:

Function showalert ()
{
Alert ("OK ");
}
Window. setTimeout (showalert, 5000 );


Cleartimeout

Clears the timer set by the setTimeout function.

Function showalert ()
{
Alert ("OK ");
}
VaR TT = Window. setTimeout (showalert, 5000 );

Window. cleartimeout (TT );

1. setTimeout, cleartimeout: Set to suspend

Use the setTimeout () method of the window object to set the pause. This method accepts two parameters: the code to be executed and the number of milliseconds (1/1000 seconds) to wait before it is executed ). The first parameter can be a code string (the same as the eval () function parameter) or a function pointer. For example, the following code displays a warning after 1 second:

SetTimeout ("alert ('Hello world! ') ", 1000 );
SetTimeout (function () {alert ('Hello world! ');}, 1000 );

Of course, you can also reference previously defined functions:
Function sayhelloworld ()
{Alert ("Hello world! ");}
Settime out (sayhelloworld, 1000 );

When setTimeout () is called, it creates a digital pause ID, which is similar to the process ID in the operating system. The pause ID is essentially the ID of the process to be delayed. After you call setTimeout (), you should not execute its code. To cancel a pause that has not been executed, call the cleartimeout () method and pass the pause ID to it:
VaR itimeoutid = setTimeout ("alert ('Hello world! ') ", 1000 );
Alert (itimeoutid );
Cleartimeout (itimeoutid );

Here, itimeoutid is a string of numbers, for example, itimeoutid played by the above Code.
You may ask, "Why do you need to define pause and cancel it before executing it ?" Consider the tooltip visible in most applications. When you move the mouse over a button, wait for a while and wait for the yellow text box to prompt the button function. If you just temporarily move the mouse over the button and then quickly move it to another button, the tooltip for the first button will not be displayed, this is the reason to cancel the code before it is paused. Because you only want to wait for the specified time before executing the code. If your operation produces different results, you need to cancel the pause.

2. setinterval and clearinterval
The interval is similar to the paused running mode, but it repeats the specified code once every specified period of time. You can call the setinterval () method to set the time interval. Its parameters are the same as setTimeout (), which is the number of milliseconds between the code to be executed and the waiting time between each execution. For example:
Setinterval ("alert ('Hello world! ') ", 1000 );
Setinterval (function () {alert ("Hello world! ") ;}, 1000 );
Function sayhelloworld ()
{Alert ("Hello world! ");}
Setinterval (Safety helloworld, 1000 );
In addition, like setTimeout (), the setinterval () method also creates an interval ID to identify the code to be executed. The clearinterval () method can use this ID to prevent the code from being executed again. Obviously. This is more important when the time interval is used, because if the time interval is not canceled, it will be executed until the page is uninstalled. The following is a common example of interval usage:
VaR inum = 0;
VaR imax= 100;
VaR iintervalid = NULL;
Function incnum ()
{Inum ++;
If (inum = IMAX)
{Clearinterval (iintervalid );}}
Iintervalid = setinterval (incnum, 500 );
In this Code, an incremental operation is performed on the number inum every 500 milliseconds until it reaches the maximum value (IMAX), and the interval is cleared. You can also use pause to perform this operation, so that you do not have to trace the ID of the time interval. The Code is as follows:
VaR inum = 0;
VaR imax= 100;
Function incnum ()
{Inum ++;
If (inum! = IMAX)
{SetTimeout (incnum, 500 );}}
SetTimeout (incnum, 500 );
This Code uses the link to pause. That is, setTimeout () is called by the code page executed by setTimeout (). If inum is not equal to IMAX after incremental operations are performed, the setTimeout () method is called. You do not need to trace the pause ID or clear it because after the code is executed, the pause ID will be destroyed.

The clearinterval () method can cancel the timeout set by setinterval.
The parameter of the clearinterval () method must be the id value returned by setinterval.
Instance
The following example calls the clock () function every 50 milliseconds. You can also use a button to stop the clock:
<Input type = "text" id = "Clock" size = "35"/>
<Script language = JavaScript>
VaR Int = self. setinterval ("clock ()", 50)
Function clock ()
{
VaR T = new date ()
Document. getelementbyid ("http://www.jinyuanbao.cn/"). value = T
}
</SCRIPT>
<Button onclick = "Int = Window. clearinterval (INT)"> stop interval </button>

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.