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); } |