Ajax cornerstone script asynchronous concurrent call parameter transfer

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. It seems that there is no Ajax client library created by VBScript, even if it only supports IE. 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. The alert result obtained by executing foo 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 );
} ;
Window. 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 complete the operation, and then ensure thatParallelTo run the dotick method. 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 in JScript ProgrammingDangerousWith advanced technology, improper use can easily lead to 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.