Differences between prototype and Ajax implementation in jquery

Source: Internet
Author: User

First, let's list the implementation of Ajax in Jquery and prototype.
Jquery:
Copy codeThe Code is as follows:
<Script language = "javascript">
$ (Function (){
Var box = {};
Var remoteUrl = 'index. php ';
Box. interval = 5x60*1000; // 5 minutes
Box. showBoxInfo = function (){
JQuery. get (remoteUrl, function (data ){
Var msg_box = $ ('# msg_box ');
Msg_box.innerHTML = data;
}
});
}
Box. run = function (){
This. showBoxInfo ();
SetInterval (this. showBoxInfo, this. interval );
};
Box. run ();
})
</Script>

Prototype:
Copy codeThe Code is as follows:
<Script language = "javascript">
Var box = {};
Box. interval = 5x60*1000; // 5 minutes
Box. showBoxInfo = function (){
Var pars = "";
Var remoteUrl = 'index. php ';
Var myAjax = new Ajax. Request (
RemoteUrl,
{
Method: 'get ',
Parameters: pars,
OnComplete: box. showResponse
});
};
Box. showResponse = function (data ){
Var msg_box = $ ("msg_box ");
Msg_box.innerHTML = data. responseText;
};
Box. run = function (){
This. showBoxInfo ();
SetInterval (this. showBoxInfo, this. interval );
};
Box. run ();
</Script>

The prototype cannot be refreshed after the setInterval function is introduced. Why?
It turns out that prototype has a caching mechanism for the same URL. Therefore, the page cannot be refreshed, especially when users use F5 or click "refresh", there will be a bug in the Ajax response area, in this way, prototype must not be refreshed.
Add random numbers for URLs in prototype
Original: var remoteUrl = 'index. php ';
After modification: var remoteUrl = 'index. php? Rand = '+ Math. random ();
You can use GET to generate a parameter irrelevant to page display.

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.