views:

276

answers:

4

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.

+1  A: 

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

Bloeper
+3  A: 

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.

cballou
Your code will call `updateDivContent` multiple times every 5 seconds. The `to` variable and the second call the `setInterval` are not necessary. Perhaps you meant to use `setTimeout`, in which case you would also have to use `clearTimeout`.
brianpeiris
Thanks for the ans.its working but after first 5 seconds it will create the copy of htmlwhat i will do for this?
Pankaj Mishra
Thanks @brian. I must not have been paying close attention when I wrote this in the morning.
cballou
@Pankaj: I fixed the code to first empty the contents of the container prior to loading any new data. This should fix your issue.
cballou
+1  A: 

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

MightyE
Actually its create page html twice.What i will do for this.
Pankaj Mishra
I'm not sure I understand the question. Do you maybe have two target `<div>` s which are both picked up by the `$('someSelector').load()` method (ie, your selector is `.classname`, and you have two divs with that class)?
MightyE
A: 

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

Pankaj Mishra