jquery realizes Ajax timed refresh local page instance _ajax related

Source: Internet
Author: User
Tags setinterval jquery library

Method One:

Local refresh we are talking about Ajax, of course, we can not use Ajax to refresh the page, we can use the append in jquery to add something to the specified content, of course, the most practical Ajax loading data.

example, timed local refresh

Timed local refresh using jquery inside the SetInterval method, SetInterval method two parameters, the first is to set the function name of the timer, the second is time, as shown in the code below, set every 10 microseconds timed to perform a AA method.

<script>
$ (function () {
 setinterval (aa,10);
 function aa () {
   $ ("#aa"). Append ("FDSADFSA");
 }
)
</script>

Example 2

 
 

Example 3 Let's take a look at how this refresh method is implemented.

JSP page Ajax

$ ("#waitWork"). Click (function () {
 var url = "Request Address";
 var data = {Type:1};
 $.ajax ({
  type: "Get",
  async:false,//sync request
  Url:url,
  data:data,
  timeout:1000,
  success: function (dates) {
  //alert (dates);
  $ ("#mainContent"). HTML (dates)//the div to be refreshed
  ,
  error:function () {
        //alert ("Failed, please try again later!"). ");
      }
 });
 });

Html

<div id= "MainContent" >

Note: The background needs for the information div in a separate JSP page, or you need to encapsulate the required page information back

Method Two:

Sometimes, I need some kind of mechanism to keep refreshing the pages to provide a real-time dashboard. If I can only refresh a particular part of the page, it will be great, for example: the traffic lights on the dashboard show the system state.

It is easy to refresh a part of the page by using the jquery JavaScript library only. Once we've incorporated our page into the jquery library, we only need 1 lines of

JavaScript gets it's work:

<script src= "/js/jquery-1.3.2.min.js" type= "Text/javascript" ></script>

So we just put our page into this little JS code snippet to refresh the contents of the ID tag in everything, let's say, every 5 seconds:

SetInterval (function () {
  $ ("#content"). Load (location.href+ "#content >*", "");
}, 5000);

That's it!! Therefore, it is very easy to complete some real-time monitoring behavior, just that line of code. No more bizarre meta refresh tags or iframe a workaround in the Web application.

Every 5 seconds, we will refresh the same URL and all elements that reside in the Content element ID: content.

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.