0

I'm trying to use jQuery to make a slightly more sophisticated page refresh. With a meta refresh, if a page fails to load once, it won't load again. I am trying to make something that will repeatedly try to load--if it fails to load once, it will try again the next time so that if the last load is successful I will see a fresh version of the page, whether or not there were intervening faults.

My code at present is:

<script language="JavaScript" type="text/javascript" src="/include/jquery.js">
</script>
<script language="JavaScript">
    var delay=5*1000;
    function load(){
        setTimeout(load, delay);
        jQuery("#content").load("calendar_internal.cgi?days=40", function(){
            jQuery("#preload").hide("slow");
        });
        delay = 15*60*1000;
    }
    jQuery("#content").load("calendar_internal.cgi?days=40", load);
</script>

So far as I can tell, this is functioning as a noop. It doesn't refresh.

How can I get this to work at least at a basic level so it refreshes but one bad refresh doesn't mean that I have to reload if I want to see the page at all?

--EDIT--

What I have now is apparently working (after light testing):

<script language="JavaScript">
var placeholder = jQuery('<div></div>');
function load(){
    setTimeout(load, 15*60*1000);
    placeholder.load("logistic_ajax.cgi", function(){
        if (placeholder.html()) 
            jQuery('#content').html(placeholder.html());
    })
}
load();
</script>
4
  • 1
    Why are you calling the load function in setTimeout in the first line of the load function?
    – Abbas
    Jan 2, 2012 at 21:14
  • For stability. The thought is that first you set up the next run, and then you attempt the network load, so that if the network load fails, you haven't lost more than a single attempted load. Jan 4, 2012 at 0:05
  • Maybe I am not seeing the logic here but isn't this an infinite recursive call? The first line in the load function calls setTimeout which calls the load function and so on.
    – Abbas
    Jan 4, 2012 at 0:10
  • The first load calls setTimeout and finishes. The second load calls setTimeout and finishes. The third load calls setTimeout and finishes. It is infinite recursion, but without infinite depth on the stack. Jan 4, 2012 at 14:46

1 Answer 1

1
var $placeHolder = $('<div />');
$placeHolder.load(yourURL,function() {
    if ($placeHolder.html()) {
        $("#content").html($placeHolder.html());
    }
});

or something like that. This way you are not doing all or nothing type of thing. If something messes up with the HTML coming back, you can check in your "if" condition. Not sure the particulars of the response so you would need to hash that out on your own or let me know more detail.

Cheers.

2
  • Hmm, it still doesn't seem to load pages; I get an empty page with the skeletal stuff outside of the load area. Jan 8, 2012 at 23:33
  • @JonathanHayward you can see a live version here -> kitgui.com/docs view source and see javascript declared, its in /js/docs/js I believe. There is something you are doing that I'm not aware of such as server-error? selector errror? etc etc so many things can go wrong. Jan 9, 2012 at 0:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.