JavaScript settimeout after deep recursive settings are complete callback

Source: Internet
Author: User
Tags javascript settimeout

SetTimeout at execution time, after the specified time, after loading, to execute an expression, execute once
SetTimeout at execution time, it executes an expression at a specified time, after it has been loaded

1, Basic usage:
Execute a piece of code:

  code is as follows copy code

    VAR i=0;
   settimeout ("I+=1;alert (i)", 1000);
   execute a function:
   var i=0
   settimeout (function () {I+=1;alert (i);},1000);
  
  //Note the difference between the two methods above.

   One more execution function:
   var i=0;
   function Test () {
   & nbsp;   i+=1;
       alert (i);
  }
   settimeout ("Test ()", 1000);
   can also be like this:
   settimeout (test,1000);

Summarize:
The prototype of settimeout is this:

The code is as follows Copy Code

Itimerid = Window.settimeout (Vcode, Imilliseconds [, Slanguage])

There are two forms of settimeout

SetTimeout (Code,interval)
SetTimeout (Func,interval,args)

Where code is a string
Func is a function.

Note that the meaning of "function" is an expression, not a statement.
Like you want to perform a function periodically

The code is as follows Copy Code
function A () {
//...
}
can be written as
SetTimeout ("A ()", 1000)
Or
SetTimeout (a,1000)

Note that in the second form, it is a, do not write a (), remember!!!


In JavaScript, give a deep recursive setting that uses settimeout, and then callback after completion


Problem:

Because the recursion is too deep and the use of asynchronous settimeout clean air-conditioning stack, the entire recursive into asynchronous behavior, the function has been returned in advance, now how to set the entire recursive completion of the callback?

Reply:

Read the question to understand your meaning, I wrote for your purpose can debug the code, you look at the problem with the code is not the same:

The code is as follows Copy Code

No callback
function IsArray (o) {
return tostring.apply (o) = = ' [Object Array] ';
}
function foo (arr) {
Console.log (arr);
if (IsArray (arr)) {
For (i in arr) {
(function (j) {
settimeout (function () {
Foo (arr[j]);
}, 0);
}) (i);
}
}
}
Foo ([[1, 2], [3, 4]]);

/*
Output:
[[1,2],[3,4]]
[1,2]
[3,4]
1
2
3
4
*/

There's a callback.
function IsArray (o) {
return tostring.apply (o) = = ' [Object Array] ';
}
Sets a counter that identifies the "number of known elements that have not been traversed" and the starting value is obviously 1
var cbcounter = 1;
function foo (arr, CB) {
Cbcounter + = IsArray (arr)? arr.length:0; The number of the handle element plus, because the child element is now Cicada
Console.log (arr);
if (IsArray (arr)) {
For (i in arr) {
(function (j) {
settimeout (function () {
Foo (Arr[j], CB);
}, 0);
}) (i);
}
}
if ((--cbcounter = = 0) && (typeof cb = = ' function ')) CB (); The front--just dig yourself out.
}
Foo ([[1, 2], [3, 4]], function () {
Console.log (' I am a callback! ');
});

/*
Output:
[[1,2],[3,4]]
[1,2]
[3,4]
1
2
3
4
I am a callback!
*/

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.