The timer in JS, the delay device

Source: Internet
Author: User
Tags set time setinterval
JS Timer, retarder 2009-11-13 18:08

The Timer collection in JavaScript
Timer: (Delay device)
Used to specify a program to be executed after a certain period of time.

SetTimeout (): (1.0 edition)
Format:

[Timer Object name =] SetTimeout ("< expression >", ms)

Function: Execute < expression > once.

SetTimeout (Code,time)

Example: settimeout ("Test for delay"); ", 10000)

Where code is a JavaScript instruction that shows the events that start after the delay.
Time is how long after the delay, 1000 represents 1 seconds.

<script language= "JavaScript" >
SetTimeout ("Confirm (' 123456 ')", 1000);
</SCRIPT>

<script language= "JavaScript" type= "Text/javascript" >


function count () {
SetTimeout ("alert (' three Seconds to ')", 3000)
}

</Script>

<input type= "button" value= "timer start" onclick= "Count ()" >

Cleartimeout (): Termination timer
Format:

Cleartimeout (Timer object name)

SetInterval (): (1.2 edition)
Format:

[Timer Object name =] SetInterval ("< expression >", ms)

Function: Repeat execution < expression until the window, frame is closed, or the clearinterval is executed.

Clearinterval (): Termination timer
Format:

Clearinterval (Timer object name)

Example 1:
<script><script language= "JavaScript" type= "Text/javascript" >


var sec = 0;
Timerid = SetInterval ("Count ()", 1000);

function count () {
num.innerhtml = sec++;
}

</Script>

Stay time:
<font id= "num" face= "impact" >0</FONT> seconds
<input type= "button" value= "Stop" onclick= "Clearinterval (Timerid)" >

Example 2:
<script language= "JavaScript" type= "Text/javascript" >


var str = "This is an online auction site, please enjoy your shopping." ";
var seq = 0;

function Scroll () {
msg = str.substring (0, seq+1);
banner.innerhtml = msg;
seq++;
if (seq >= str.length) seq = 0;
}

</Script>

<body onload= "setinterval (' scroll () ')" >
<font id= "banner" ></FONT>
</Body>

Article references from:


SetInterval and Clearinterval explain July 10, 2007 Tuesday afternoon 05:06clearinterval similar to JS cleartimeout

The time interval can be created with the SetInterval command and terminated with the Clearinterval command. The parameters used in SetInterval are in two forms. In the first format, the argument you pass to the setinterval can be a function name, an interval over time, and some relevant arguments passed to the previous function. When the setinterval runs, it passes the listed arguments to the specified function in sequence at the prescribed time interval, until you call Clearinterval to terminate it. The relevant model code is as follows:

function Updatestockprices (whichstock) {
Update code here
Trace (' Updating prices for ' +whichstock);
}
Stockinterval = SetInterval (updatestockprices, 1000, "Stratford Flash products");


SetInterval a comprehensive introduction
The function of the SetInterval action is to call the function, method, or object at a certain time when the animation is played. You can use this action to update a variable or update time display from a database. The syntax format for the SetInterval action is as follows:
SetInterval (function,interval[,arg1,arg2,...... argn])
SetInterval (object,methodname,interval[,arg1,arg2,..... argn])
The first format is the default syntax for the SetInterval function in the standard action panel, and the second format is the method used in expert mode actions.
The parameter function is a function name or a reference to an anonymous function. The object parameter specifies objects that derive from object objects. MethodName the method to invoke in the object argument. Interval the time between two calls to a function or methodname, in milliseconds. The arg1, and so on, are optional parameters that can be used to make arguments passed to a function or methodname.
SetInterval It is set to a time interval smaller than the animation frame rate (such as 10 frames per second, equivalent to 100 milliseconds), the function is called at intervals as close to interval as possible. You must also use the Updateafterevent action to ensure that the screen is refreshed with sufficient frequency. If the interval is larger than the animation frame rate, it is invoked only when a frame is entered for each playhead to reduce the effect of each refresh screen.
The following example calls an anonymous function every 1 seconds.

SetInterval (function () {Trace (I will show every 1 seconds)},1000);//The function () {} is not a function name
The function. As an anonymous function, the following 1000 is the time interval, in milliseconds.
The following example shows us how to run with parameters.

function Show1 () {
Trace ("I will show it once every 1 seconds");
}
function Show2 (str) {
Trace (str);
}
SetInterval (show1,1000);
SetInterval (show2,2000, "I will show every 2 seconds");
The SetInterval method of the function is described above.
Here we'll introduce the SetInterval method of the object.
First, write an example of a method that setinterval the object in action, which does not require passing arguments.

Myobj=new object ()///Create a new one
Myobj.interval=function) {
Trace ("I will show it once every 1 seconds");
}//The method that creates the object.
SetInterval (myobj, "interval", 1000);//Set time interval to invoke the object's method.

Next, we'll describe how to pass parameters. In fact, the argument is the same as the passing parameters of the function.

Myobj=new Object ();
Myobj.interval-function (str) {
Trace (str);
}
SetInterval (myobj, "Interval", 2000, "I will show every 2 seconds");

Attention. To invoke a method defined for an object, you must use the second syntax format in expert mode.
In this case, we will make a dynamic display of the time picture. This can be achieved using the following code.

SetInterval (show,1000);
Function Show () {
Time=new Date ();
Hour=time.gethours ();
Minu=time.getminutes ();
Sec=time.get.seconds ();
Datetime=hour ":" Minu ":" SEC;
}//here the datetime is the variable name of a dynamic text box.

This way, setinterval this method everybody should learn very well. Now, we learn clearinterval.
Clearinterval action is a clear call to the SetInterval function, its syntax format is as follows Clearinterval (intervalid); Intervalid is the object returned after invoking the SetInterval function.
Here's a simple example.

Function Show () {
Trace ("I will show it once every second");
}
var sh;
Sh=setinterval (show,1000);
Clearinterval (SH);

Category: Java Programming | | Add to Search | Share to I bar | browse (231) | Comments (0) Previous: JSP page use FCKeditor control Next: Struts Label use example--logic article

Related articles:

09. Timer: (Delay device)

Recent Readers:

You are here when the
is logged in.  
    lim_ao luoqinglong

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.