This introduction of setTimeout and setInterval in Javascript objects. For more information, see setTimeout and setInterval in Javascript. The first parameter received by setInterval is a string or function, when setTimeout is used to call the method of this object in an object
The Code is as follows:
Function obj (){This. fn = function (){Alert ("OK ");Consol
The setTimeout usage of the timer in javascript is generally as follows. After beginrotate is called, a process of regularly executing rotateloop is started. The following code:Copy codeThe Code is as follows:Var angle = 0;Function rotateloop (){If (angle Angle ++;// Use angle//......SetTimeout ("rotateloop ()", 100 );}}Function beginrotate (){// Do something//......Set
Basic usage of settimeout and setinterval we don't talk about it, it's nothing but 1. Specify the delay after calling the function, 2. Call a function with a specified periodLet's imagine an unexpected situation, such as the following setinterval
setInterval(function(){func(i++);},100)
We call the Func function every 100 milliseconds, and if the Func execution time is less than 100 milliseconds, it will be able to execute
It may even mistakenly understand the two functions that implement scheduled calls as something similar to thread, and think that they will execute the called functions concurrently in a time slice, which seems very good and powerful, however, this is not the case. The actual situation is that javascript runs in the browser's javascript engine in a single thread, the functions of setTimeout and setInterval are to insert the code you want to execute in
JS manual»settimeout (): Used to call a function or evaluate an expression after a specified number of milliseconds;English explanation»timeout (): timeout, temporary rest, breaks;JS manual»setinterval (): Invokes a function or evaluation expression in the specified period (in milliseconds). The function is called continuously until the Clearinterval () is called or the window is closed;English explanation»interval (): interval; spacing; intermission;
One, settimeout this point to the problem
This in settimeout ("This.count ()", 1000) refers to the Window object.
The settimeout of JS is defined as
Copy Code code as follows:
Window.settimeout=function (Vcode, Imilliseconds [, Slanguage]) {
//..... Code
Return timer//Returns a marker
}
So when you pass this to
, and these pits may not be from the current new thing, but they have some basic knowledge is not solid.
function Asyncfunction () {return
new Promise (function (resolve, reject) {
settimeout (function () {
Resolve (' Async Hello World ')
;} Asyncfunction (). Then (function (value) {
console.log (value); => ' Async Hello World '
}. catch (function (error) {
consol
>show an alert box after 2 secondsTips for using settimeout
(1) The call function pass parameter in IE to the settimeoutThe above analysis shows that IE is not supported in the settimeout to the called function parameters, for the sake of the harmony of the browser world, we can wrap the function call parameters into the new anonymous function. Example:function f (a) {alert (a);}
The use of timer settimeout in JavaScript is generally as follows: After calling Beginrotate, you enter a process that executes the rotateloop in a timed, following code.
Copy Code code as follows:
var angle = 0;
function Rotateloop () {
if (angle angle++;
Use angle
//......
SetTimeout ("Rotateloop ()", 100);
}
}
function Beginrotate () {
Do something
//......
Currently this settimeout can be well compatible with ie6,7,8,9 as well as Google browser Chrome, Firefox firefox, Apple browser Safari,opera.
SetTimeout is a very good function, Site page front-end engineers often use it for a few seconds after the execution of the action. settimeout This JS built-in function is also very simple to use, the following is the
Original: JavaScript Tour-terminal: the confused settimeout Sometimes the end is not very good, but at least it is a kind of ending, this series of the last article settimeout, this is a confusing function, but also I have been in the slot JSReason, we do not see the source code JS, Setimeout also, from the beginning to the end is the use of black boxes.A: settimeout
In JS, whether it is settimeout or setinterval, when using the function name as the call handle can not take parameters, and in many cases must be with parameters, this need to solve the method.
Today in writing a JS delay event found in settimeout this method using JQuery's $ (this) unexpectedly does not work, various tests, and finally concluded that the settimeout
When it comes to setinterval, you have to mention settimeout, both of which are used to perform a function on a regular basis, the difference being that settimeout is executed only once, and setinterval can be executed continuously, typically as follows:
function Do_sth () {console.log (' Hello ... ');}
settimeout (Do_sth, 2500); After 2.5
SetTimeout and SetInterval are two very magical ways of window objects that enable you to call a function or a piece of code periodically or in a delay.
(Novice may think that settimeout and setinterval are JavaScript functions, which is wrong.) Beginners can easily confuse JavaScript object functions with Dom object methods. )
First a piece of code, you can guess what the results will be in various brows
This article mainly introduces the setTimeout () and clearTimeout () usage in js, and analyzes the functions and usage skills of setTimeout () and clearTimeout () in the form of instances, for more information about setTimeout () and clearTimeout () usage in js, see this article. It analyzes the functions and usage skills of
When using the closure feature of setTimeout () in Javascript, you need to pay attention to the problem.
SetTimeout is often used to delay the execution of a function. Its usage is:
Copy codeThe Code is as follows:SetTimeout (function (){...}, Timeout );
Sometimes setTimeout (function ..., 0); for example:
Copy codeThe Code is as follows:Function f (){... // Get
In JS, whether it is setTimeout or setInterval, parameters cannot be included when using the function name as the call handle. In many cases, parameters must be included, which requires a solution. Today, when I wrote a js latency event, I found that the $ (this) of jquery In the setTimeout method did not work. Various tests, the final conclusion is that the setTimeout
This article mainly introduces the js timeout call setTimeout and the intermittent call setInterval. It compares and analyzes the specific usage skills of setTimeout and setInterval in the form of instances, which is very useful, for more information about how to use setTimeout and setInterval for intermittent calls, see the example in this article. Share it with
SetTimeout (code, millisec) method. If the code of the 1st parameter calls the function name directly, a global or local function call problem exists. Note the following when using the setTimeout (code, millisec) method:
SetTimeout (expression, interaction time)The latency/interaction time is in the unit of haoss (1000 ms = 1 s). When
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.