Pankaj Mishra
Pankaj Mishra

Reputation: 20348

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.

Upvotes: 3

Views: 2801

Answers (4)

Pankaj Mishra
Pankaj Mishra

Reputation: 20348

$(document).ready(function() {
        var refreshId = setInterval(function()
        {
             $('#main').fadeOut("slow").load('Default.aspx').fadeIn("slow");
        }, 50000);
        });

Upvotes: 5

Corey Ballou
Corey Ballou

Reputation: 43457

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.

Upvotes: 4

MightyE
MightyE

Reputation: 2679

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).

Upvotes: 1

Bloeper
Bloeper

Reputation: 357

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.

Upvotes: 1

Related Questions