Ajax cornerstone: asynchronous and concurrent parameter transfer of scripts

Source: Internet
Author: User
In the Ajax development framework, the most basic division is server and client. The server side is relatively simple, as long as the language for developing dynamic web pages is competent; the client browser is the world of JScript/JavaScript, and it seems that there is no Ajax client library created by VBScript. Because the client depends on scripts and runs in a browser, it seems to be less feasible and manageability than the server.

Here I will talk about some problems in calling a and asynchronous in Ajax. If we do not pass any parameters for the method during asynchronous calls, the problem will be simple or even non-existent. But in fact, when we develop some slightly more complex functions, it is very necessary for the "Asynchronous and concurrent call of parameter passing in scripts" function. To be more serious, we can regard this function as the cornerstone of whether the Ajax client framework can truly be asynchronous. For the problem of passing asynchronous call parameters, refer to my previous article.Article"Passing parameters for setinterval using anonymous functions ". Although the example in this article solves the parameter passing problem, let's take a look at the following example and see what we will find?

Function Foo ()
{
VaR Param = 100;
Window. setTimeout (function ()
{
Intervalrun (PARAM );
}, 0 );
Param = 0;
}

Function intervalrun (times)
{
Alert (times );
}

What alert results will we get when we execute Foo? 100? Or 0 ?, The answer is: 0. As a matter of fact, if you have written an embedded function, you will know that 100 is required here. You need to rewrite the foo method as follows:

Function Foo ()
{
VaR Param = 100;
VaR _ Param = Param;
Window. setTimeout (function ()
{
Intervalrun (_ PARAM );
}, 0 );
Param = 0;
} // Add a variable to store Param. Then, execute Foo and the alert result is: 100.

The above correction is correct, but if I execute it concurrently, there may be new problems. ExampleCode:

Function dotick ()
{
VaR tick = new date (). gettime ();
VaR _ tick = tick;
VaR Foo = function ()
{
Gettick (_ tick );
};
Windows. setTimeout (Foo, 3000 );
}

Function gettick (tick)
{
// To Do Something depend on tick Parameter
}

When we call the dotick method, if the concurrency is less than ms, the previous tick variable will be modified by the subsequent execution process, as a result, the gettick method gets the wrong tick parameter. That is to say, you must regard the dotick method as a method that requires "self-execution time + 3000 ms" to run completely, and then ensure that the parallel execution of the dotick method will not go wrong. Such restrictions are obviously unacceptable. What should we do?

In fact, we only need to use nested functions to help us pass parameters. 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 );
}

Since the nested function constructs a closure scope, it will help us save the context of the parameter, so that we can get the real "asynchronous concurrent call parameter transfer" effect.

Note: Closure scope is a dangerous advanced technology in JScript programming. improper use can easily cause IE memory leak.

Trackback: http://tb.blog.csdn.net/TrackBack.aspx? Postid = 895820

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.