Build a pool to manage the xmlhttp object of the refreshing page

Source: Internet
Author: User

ActiveXObject is very expensive on pages. If we use xmlhttp Technology for refreshing pages, we may need to frequently create xmlhttp objects, of course, we can also use global variables to cache an xmlhttp object instance. However, this method is suitable for xmlhttp communication in synchronous mode, while xmlhttp communication in asynchronous mode may fail. Because there is no process congestion, the user may call the same xmlhttp instance again. If the previous communication is not completed, it will fail.

Create a pool to manage xmlhttp object instances on the page. The most obvious benefit is that no redundant objects will be created, at the same time, multiple operations will not be called on the xmlhttp instance of the same job.

For specific implementation, we use an Array as the pool to store the created xmlhttp object instance, and each call retrieves an instance from the pool. The xmlhttp instance does not need to be disposed of after communication, because its readyState attribute can identify whether it is available. If there is no idle xmlhttp instance at the time and the number of instances in the pool is less than m_MaxPoolLength, create a new instance and put it in the pool. The pool implementation code is as follows:

_ XmlHttpPool _ source code # region _ XmlHttpPool _ source code
Var _ XmlHttpPool _ =
{
M_MaxPoolLength: 10,
M_XmlHttpPool: [],

_ RequestObject: function ()
{
Var xmlhttp = null;
Var pool = this. m_XmlHttpPool;
For (var I = 0; I <pool. length; ++ I)
{
If (pool [I]. readyState = 4 | pool [I]. readyState = 0)
{
Xmlhttp = pool [I];
Break;
}
}
If (xmlhttp = null)
{
Return this. _ extendPool ();
}
Return xmlhttp;
},

_ ExtendPool: function ()
{
If (this. m_XmlHttpPool.length <this. m_MaxPoolLength)
{
Var xmlhttp = null;
Try
{
Xmlhttp = new ActiveXObject ('msxml2. xmlhttp ');
}
Catch (e)
{
Try
{
Xmlhttp = new ActiveXObject ('Microsoft. xmlhttp ');
}
Catch (e2 ){}
}
If (xmlhttp)
{
This. m_XmlHttpPool.push (xmlhttp );
}
Return xmlhttp;
}
},

GetRemoteData: function (url, callback)
{
This. _ receiveRemoteData (url, callback, 'get', null );
},

PostRemoteData: function (url, callback, data)
{
This. _ receiveRemoteData (url, callback, 'post', data );
},

_ ReceiveRemoteData: function (url, callback, httpmethod, data)
{
Var xmlhttp = this. _ requestObject ();
If (! Xmlhttp)
{
Return null;
}
Xmlhttp. open (httpmethod, url, true );
Xmlhttp. onreadystatechange = function ()
{
If (xmlhttp. readyState = 4 | xmlhttp. readyState = 'complete ')
{
Callback (xmlhttp. responseText );
}
};
Xmlhttp. send (data );
}
};
# Endregion

_ XmlHttpPool _ provides two methods to communicate with the server. One uses the 'get' method and the other uses the 'post' method, which is very simple to use, __xmlhttppool __. getRemoteData (url, callback) or _ XmlHttpPool __. postRemoteData (url, callback, data ). The url is the server address, and the callback is the callback function for processing the returned data (responseText. For example:

<Script language = "javascript">
_ XmlHttpPool _. PostRemoteData (url, Render, 'abc ');

Function Render (string)
{
If (string)
{
TxbContent. value + = string + '\ r \ n ';
}
}

</Scirpt>
<Textarea rows = "40" style = "width: 100%" id = "txbContent"> </textarea>

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.