Ajax| Scripts | Asynchronous in the AJAX development Framework, the most basic division is the server side and the client. Server-side relatively simple, as long as the Dynamic Web page can be developed by the language can be competent; The client browser is Jscript/javascript, as if not see the AJAX client library with VBScript. Because the client relies on scripting and runs in the browser, it seems to be worse than the server-side implementation and manageability.
Here I say some of the questions about a, asynchronous call in Ajax. If we do not pass any arguments to the method when we call asynchronously, the problem is simple or non-existent. But in fact, when we develop some slightly more complex functionality, the function of "scripting asynchronous concurrent Call parameter passing" is very much needed. Seriously, this functionality can be seen as the cornerstone of whether the AJAX client framework really asynchronous. For the problem of asynchronous call parameter passing, see my previous article "using anonymous functions to pass parameters to SetInterval". Although the example in this article is a good solution to the problem of parameter passing, let's look at the following example to see what we find?
function foo () { var param = 100; Window.settimeout (function () { Intervalrun (param); }, 0); param = 0; }
function Intervalrun (times) { alert (times); } |
What alert results do we get when we execute foo? 100? or 0?, the answer is: 0. Actually write the inline function to know, here to actually pass in 100, need to rewrite Foo method like this:
function foo () { var param = 100; var __param = param; Window.settimeout (function () { Intervalrun (__param); }, 0); param = 0; }//add a variable to store the Param so that the alert result of Foo is: 100. |
The above amendment itself is fine, but if I do it concurrently, there may be new problems. Sample code:
function Dotick () { var tick = new Date (). GetTime (); var __tick = tick; var foo = function () { Gettick (__tick); }; Window.settimeout (foo, 3000); }
function Gettick (tick) { To did something depend on tick parameter } |
When we call the Dotick method, if concurrency is less than 3000ms, it causes the previous tick variable to be modified by subsequent execution, causing the Gettick method to fetch the wrong tick parameter. That is to say, the Dotick method must be considered as a method that requires "the execution time +3000ms" to run out, and then ensure that the parallel execution of the Dotick method is not wrong. Such restrictions are clearly unacceptable, so what should we do?
In fact, we just need to use the inline function itself to help us pass parameters on the line, the modified example is as follows:
function Dotick () { var tick = new Date (). GetTime (); var foo = function () { var __tick = foo.params[0]; Gettick (__tick); }; Foo.params = [tick]; Window.settimeout (foo, 0); } |
Because the inline function constructs a closure Scope, it will help us save the context of the parameter so that we get a true "asynchronous concurrent call parameter Pass" effect.
Note: Closure scope is a risky, advanced technique in JScript programming that is highly susceptible to the use of IE Memory leak.