SetTimeout
definition and Usage:The SetTimeout () method is used to call a function or evaluate an expression after a specified number of milliseconds.
Syntax:SetTimeout (CODE,MILLISEC)
Parameters:Code (required): The JavaScript code string to execute after the function to be called. Millisec (required): The number of milliseconds to wait before executing the code.
Tips:SetTimeout () executes code only once. If you want to call more than once, use SetInterval () or let code itself call SetTimeout () again. Two types of calling functions: function page_list () {alert ("Shihuan");} Window.settimeout (Page_list, 5000);//Indicates delay 5 seconds to execute page_list () function
Window.settimeout ("Page_list()", 30000);//Indicates delay 30 seconds to execute page_list () function with parameter method using SetTimeout to note that setTimeout ("function name (" + parameter + ")", millisecond number), here the argument can only be a string form, and cannot pass an object, online many friends are also asking such questions, I hereby explain, Here are a few simple examples: Online find "with parameter SetTimeout", many friends write a lot of ways to implement the SetTimeout with the object of the method loop, for example: <script language= "JavaScript" > var __sto = setTimeout; window.settimeout = function (callback, timeout, param) { var args = Array.prototype.slice.call (arguments, 2); var _CB = function () { & nbsp; Callback.apply (null, args); } &NBSP;&NBsp __sto (_CB, timeout); } //test codefunctionShihuan(a) {alert (a); } functionYushibo(a,b,C) {alert (a+b+C); } var a = new Object (); Window.settimeout (Shihuan, 1000,a); Window.settimeout (Yushibo, 2000,a,6,7;</script> in this example, SetTimeout usage, setTimeout (callback function, time, parameter 1, ..., parameter n). --------------------------------------------------------------------------------------------------Definition and usage
The SetInterval () method invokes a function or evaluates an expression according to the specified period (in milliseconds).
The SetInterval () method keeps calling functions until Clearinterval () is called or the window is closed. The ID value returned by SetInterval () can be used as a parameter to the Clearinterval () method.
Grammar
SetInterval (code,millisec[, "Lang"])
Parameters |
Description |
Code |
Necessary. The function to invoke or the code string to execute. |
Millisec |
Have to. The time interval, in milliseconds, between periodically executing or calling code. |
return value
A value that can be passed to Window.clearinterval () to suppress periodic execution of code.
Example:
<body>
<form>
<input type= "text" id= "clock" size= "/>"
<script language=javascript>
var int=self.setInterval("clock()", 50)
function Clock () {
var t=new Date ()
document.getelementbyidx_x_x_x_x_x ("Clock"). Value = t
}
</script>
</form>
<button onclick= "int= window.clearInterval(int)">
Stop interval</button>
</body>
--------------------------------------------------------------------------------------------------
Cleartimeout () and Clearinterval ():
JS Set delay:
Using setinterval and setting the delay function settimeout very similar.
SetTimeout used to delay a period of time before doing an operation.
SetTimeout ("function", time) sets a timeout object
SetInterval ("function", time) //Set a timeout object
SetInterval for automatic repetition, settimeout does not repeat.
Cleartimeout (object) clears the SetTimeout object that has been set
Clearinterval (object) clears the SetInterval object that has been set
first, what is called Javas timing Events for Cript
Using JavaScript can implement deferred execution of code, which means that when a function is called, it does not execute some code immediately, but waits for a specified amount of time to execute, which is called a timing event.
Second, Javas cript function of the timing event
SetTimeout () //-execute code after a specified time
Cleartimeout () //-cancel SetTimeout ()
Note: setTimeout () and cleartimeout () are functions of the Window object of the HTML DOM.
Three, settimeout detailed
var t = setTimeout ("javascript statement", time parameter)
Note: The time parameter unit is milliseconds
Example: Var t=settimeout ("Alert (' 3 seconds! ')", 3000)
If the JS statement has a variable, you must concatenate the variable with the + sign, such as:
var t = setTimeout ("document.getelementbyidx_x_x_x_x_x" ("+menuid+"). style.display= ' None ' ", 3000)
Four, Cleartimeout detailed
Syntax: cleartimeout (variable name for settimeout)
Example: Cleartimeout (t) //where T is the variable of the settimeout set above
Use Cleartimeout to stop timing at any time.
V. Application Skills
It is recommended that settimeout be set individually as a function. Such as:
function Delayrun (code, time) {
var t = setTimeout (code, time);
}
This way, when you need to delay execution of a piece of code, simply add the function before the code. Such as:
onmouseover = Delayrun ("Settab (0,0)", 500)
Where Settab is a custom function. If you do not want to let settab delay execution later, then remove the Delayrun-related code in the statement,
Change to: Onmouseover=settab (0, 0) on it.
This kind of writing avoids each need to delay the place to write a piece of settimeout code, just need to call directly can, very convenient. It also saves the amount of code.
--------------------------------------------------------------------------------------------------
Self-made table: used to display the current time of the
<! DOCTYPE html>
<body>
<p>a script on the page starts this clock:</p>
<p id= "Demo" ></p>
<script>
var myvar=setinterval (function () {MyTimer ()},1000);
function MyTimer ()
{
var d=new Date ();
var t=d.tolocaletimestring ();
document.getElementById ("Demo"). Innerhtml=t;
}
</script>
</body>