Javascript timer. setinterval usage

Source: Internet
Author: User

It takes about two minutes to refresh a report at work.

You can use a javascript timer to automatically call the function as follows:

 

1. OBJ = setTimeout ("function name", time) timer, called only once; obj is the returned object;
Cleartimeout (OBJ) Cleanup Timer

 

2. OBJ = setinterval ("function", INT) timer. The function is called every integer in milliseconds until clearinterval is used to clear the timer;

Clearinterval (OBJ); clear the timer to stop calling

Clearinterval is similar to cleartimeout of Js.

You can use the setinterval command to create the interval and use the clearinterval command to terminate the interval. The parameters used by setinterval are in two formats. In the first format, the parameter you pass to setinterval can be a function name, interval over a period of time, and related parameters passed to the previous function. When setinterval is run, it passes the listed parameters to the specified function in sequence according to the specified interval until you call clearinterval to terminate it. The Demo code is as follows:

Function updatestockprices (whichstock ){

// Update code here

Trace ('updating prices for '+ whichstock );

}

Stockinterval = setinterval (updatestockprices, 1000, "Stratford FLASH products ");

Setinterval

The setinterval action is used to call a function, method, or object every certain time when an animation is played. You can use this action to update the variables or update time display from the database. The syntax format of 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 of 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 the object derived from the object. Methodname specifies the method to be called in the object parameter. Interval specifies the time between two calls to a function or methodname, in milliseconds. The following arg1 and so on are optional parameters used to specify the parameters passed to the function or methodname.

Setinterval: the time interval set by setinterval is smaller than the animation frame rate (for example, 10 frames per second, equivalent to 100 milliseconds), and the function is called at the interval as close as possible to interval. The updateafterevent action must be used to ensure that the screen is refreshed at sufficient frequency. If interval is greater than the animation frame speed, it is called only when the playback header enters a certain frame to reduce the impact of screen refreshing.

The following example calls an anonymous function every one second.

Setinterval (function () {trace ("I will display it once every 1 second")}, 1000); // The function () {} here has no function name

. To become an anonymous function, the next 1000 is the time interval, in milliseconds.

The following example shows how to run with parameters.

Function show1 (){

Trace ("I will display it once every 1 second ");

}

Function show2 (STR ){

Trace (STR );

}

Setinterval (show1, 1000 );

Setinterval (show2, 2000, "I will display it once every 2 seconds ");

The setinterval method of the function has been introduced above.

Next we will introduce the setinterval method of the object.

First, write an example of setinterval to call the object method in an action. This example does not need to pass parameters.

Myobj = new object (); // create a new object

Myobj. interval = function ){

Trace ("I will display it once every 1 second ");

} // Method for creating an object.

Setinterval (myobj, "interval", 1000); // sets the interval to call the object method.

Next we will introduce how to pass parameters. In fact, the principle is the same as the parameter passed by the function.

Myobj = new object ();

Myobj. interval-function (STR ){

Trace (STR );

}

Setinterval (myobj, "interval", 2000, "I will display it once every 2 seconds ");

Note. To call a method defined as an object, you must use the second syntax format in EXPERT mode.

In this case, we will make a dynamic display of time. You can use 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;

} // The datetime here is the variable name of a dynamic text box.

In this case, the setinterval method should be well learned. Now, let's learn clearinterval.

Clearinterval is used to clear the call to the setinterval function. Its Syntax format is clearinterval (intervalid). intervalid is the object returned after the setinterval function is called.

The following is a simple example.

Function show (){

Trace ("I will display it once every second ");

}

VaR sh;

SH = setinterval (show, 1000 );

Clearinterval (SH );

In addition, if you need to call JavaScript Functions and input parameters, you can write the following statements:

Function Test (DT ){
Alert (DT + "joch ");
}

Window. setinterval (function () {test (starttime) ;}, 5000); // starttime is a parameter.

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.