SetInterval and settimeout usage in JS

Source: Internet
Author: User

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. Tip: setTimeout () executes code only once. If you want to call more than once, use SetInterval () or let code itself call SetTimeout () again.  two ways to call 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 the parameter method using SetTimeout to note that the SetTimeout ("function name (" + parameter + "), the number of milliseconds), 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 () {callback.apply (null, args); } __sto (_CB, timeout); } //test code   function Shihuan(a) {alert (a); }function Yushibo(a, b, c) {alert (a + b + c); }var a = new Object (); window.settimeout (Shihuan, N, a); window.settimeout (Yushibo, a, 6, 7);</script> in this example, SetTimeout usage, setTimeout (callback function, time, parameter 1, ..., parameter n). --------------------------------------------------------------------------------------------------setintervalDefinition 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_xx_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: use setinterval and set 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_xx_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.

SetInterval and settimeout usage in JS

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.