Settimeout,setinterval timed operation in JavaScript

Source: Internet
Author: User
Tags set time setinterval time interval

Today, you are experiencing a problem with a timed operation in your project. JavaScript provides two methods for timed operations:

The code is as follows Copy Code
1, Timeid = settimeout ("function ();", delaytime);

Timeid is the ID of the timer (used later),

The Delaytime unit is 1 per thousand seconds, millisecond.

The code is as follows Copy Code

2, Timeid = SetInterval ("function ();", delaytime); Ditto

The difference between the two timers is that the settimeout () executes the specified method (called the timer) after a period of time, and SetInterval executes the specified method (called the cycle timer) every once in a while.

For the inverted timer, after loading 1 seconds after the pop-up dialog box, demo code:

The inverted timer, after loading 1 seconds after the pop-up dialog box, demo code:

The code is as follows Copy Code

<script lang= ' JavaScript ' >

Timeid = settimeout ("alert (' OK ');", 1000);

</script>

For the loop timer: after loading every 1 seconds after the pop-up dialog box, demo code:

The code is as follows Copy Code

<script lang= ' JavaScript ' >
Timeid = SetInterval ("alert (' OK ');", 1000);
</script>


Since the timer ID is mentioned above, then this ID must have his usefulness, this ID will be in the process of the timer to meet certain conditions to close this timer, the corresponding also provides two methods:

1, cleartimeout (Timeid);

2, Clearinterval (Timeid);

Now you can try to add a timed operation on the website, if we want to implement the indefinite operation,

Use SetTimeout (callback, delay, [ARG], [...]) Performs the specified function after the set time interval. parameter is similar to the SetInterval function. Also returns a Timeoutid that is used to invoke Cleartimeout (Timeoutid) to stop this trigger from being terminated.

Therefore, we can take advantage of the characteristics of settimeout, to implement the function of a certain operation when uncertain.

The code is as follows Copy Code

var cruisenodes = [1,2,1,3];
var Cruiseno = 0;

Startplaycruise ();

function Startplaycruise () {
if (Cruiseno >= cruisenodes.length) return;

Cruisedelay = parsefloat (Cruisenodes[cruiseno]) *1000;

alert (Cruisedelay);

cruiseno++;

SetTimeout (Startplaycruise, Cruisedelay);
}


The settimeout function call that passes the parameter to the callback function is written as:

The code is as follows Copy Code

SetTimeout (MyFunction, mytimeout, parameter);

The sample code is as follows:

The code is as follows Copy Code

var cruisenodes = [1,2,1,3];
var Cruiseno = 0;

Startplaycruise (Cruiseno);

function Startplaycruise (number) {
if (Cruiseno >= cruisenodes.length) return;

Cruisedelay = parsefloat (Cruisenodes[cruiseno]) *1000;

Use the ordinal as the settimeout function for the input parameters of the Startplaycruise function, and display here
alert (number);

cruiseno++;

SetTimeout (Startplaycruise,cruisedelay,cruiseno);
Or use closures, as follows:
settimeout (function () (Startplaycruise (Cruiseno)), cruisedelay);
}

Related Article

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.