Usage of setInterval and setTimeout in js

1.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)   
    parameter:   
    code (required): A string of JavaScript code to execute after the function to be called.   
    millisec (required): The number of milliseconds to wait before executing code.  
    Tip: setTimeout() executes the code only once. If you want to call it multiple times, use setInterval() or have the code itself call setTimeout() again.

 

 There are two ways to call the function:

function page_list(){
   alert("shihuan");
}
window.setTimeout(page_list, 5000); //Indicates a delay of 5 seconds to execute the page_list() function
window.setTimeout("page_list()", 30000); //Indicates a delay of 30 seconds to execute the page_list() function
 

When using setTimeout in a method with parameters, it should be noted that setTimeout("function name("+parameter+")", milliseconds), the parameter here can only be in the form of a string, and an object cannot be passed.

 

2.setInterval definition and usage

 

The setInterval() method calls a function or evaluates an expression at a specified interval (in milliseconds).

The setInterval() method keeps calling the function until clearInterval() is called or the window is closed. The ID value returned by setInterval() can be used as an argument to the clearInterval() method.

Syntax: setInterval(code,millisec[,"lang"])

Parameters:
code is required. A function to call or a string of code to execute.
millisec required. The time interval, in milliseconds, between periodic executions or invocations of code.

return value:
A value that can be passed to Window.clearInterval() to cancel periodic execution of the code.

 

3.clearTimeout()和clearInterval() :

Set the delay in JS:
Using SetInterval is very similar to setting the delay function setTimeout.
setTimeout is used to delay for a period of time before performing an operation.
setTimeout("function",time) sets a timeout object
setInterval("function",time) //Set a timeout object

SetInterval is automatically repeated, setTimeout will not repeat.
clearTimeout(object) clears the setTimeout object that has been set
clearInterval(object) clears the setInterval object that has been set

4.js timing events

Using JavaScript can realize the delayed execution of code, that is to say, when a function is called, some code is not executed immediately, but after a specified period of time, it is called a timing event.

5. Timing event function

setTimeout() //- execute code after specified time
clearTimeout() //- 取消 setTimeout()

Note: setTimeout() and clearTimeout() are both functions of the HTML DOM's Window object.

 

 

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326699140&siteId=291194637