English 中文(简体)
How can i update div continuously
原标题:

I have asp.net application where i have a div which showing the value from other site. The value of that site is changing continuously.

I want that my div will automatically update in some interval time.

How can i do this.

最佳回答
$(document).ready(function() {
        var refreshId = setInterval(function()
        {
             $( #main ).fadeOut("slow").load( Default.aspx ).fadeIn("slow");
        }, 50000);
        });
问题回答

Sorry for the bug. setTimeout is used as opposed to setInterval to accomodate for any delay that may occur in the AJAX request.

var to;
$(function() {
    // initialize timer to update div every 5 seconds
    to = setTimeout(updateDivContent, 5000);
});

function updateDivContent() {
    // make your AJAX/LOAD request for the data here to populate div
    $( #mydivcontainer ).empty().load( myAspFileToGrabExternalData.aspx , null, function() {
        // reset the timer to grab the content in another 5 seconds
        to = setTimeout(updateDivContent, 5000);
    });
}

You can read up on jQuery s load method here.

You could use the setinterval function of jquery/javascript. For some information you can look at this tutorial: http://docs.jquery.com/Tutorials:Scroll_Up_Headline_Reader Or search the jquery document site for other references.

You need to use active polling (repeatedly checking the other site), which might earn you some hate from that site (as well as possibly have legal repercussions) unless you re the one who owns it. You might not want to use setInterval() to poll the other site as this could introduce race conditions if the site takes a bit to respond (i.e. if you re polling every 5 seconds, and the site takes 6 seconds to respond once, then 1 second to respond on the subsequent response, both of these will hit your page at the same time).

To borrow from cballou s post:

var to;
$(function() {
    // initialize timer to update div every 5 seconds
    to = setTimeout(updateDivContent, 5000);
});

function updateDivContent() {
    // make your AJAX/LOAD request for the data here to populate div
    $( #mydivcontainer ).load( myAspFileToGrabExternalData.aspx , null, function() {
        // reset the timer to grab the content in another 5 seconds
        to = setTimeout(updateDivContent, 5000);
    });
}

Also, cballou s post has a bug where it would have queued a new interval each time it ran (so that after 3 iterations, you re fetching the page 3 times every 5 seconds, after 10 iterations, you re fetching it twice a second, etc).





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签