Ajax Cornerstone Script Asynchronous concurrent call parameter passing

Source: Internet
Author: User
Tags closure execution


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;
}// 这样添加一个变量来存储param就可以了,这下执行foo得到的alert结果就是: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 do 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.


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.