tags:

views:

30

answers:

2

Hi!

I toyed around with .load() and .ajax(), but i didn't get to an end. Here's what I need: Everey minute the function shall check if it can load a certain page. if the connection succeeds, I want the page to be refreshed, if not, nothing shall happen, the script shall retry later.

I'm using the jQueryTimers plugin. this is my code so far:

//reload trigger
$(document).everyTime('60s', 'pagerefresh', reloadPage, 0, true);

//refresh function
function reloadPage() {
    $.ajax({
        url:'index-1.php',
        type:'HEAD',
        success: location.reload(true)
        })
}

I have no idea how to tell jQ what I want. any hint appreciated.

+9  A: 

By writing success: location.reload(), you are calling reload immediately, and setting the success parameter to the whatever reload returns (which is undefined)

You need to use a anonymous function that calls reload as the success callback, like this:

    success: function() { location.reload(true); }
SLaks
thanks for your help! that helped me out, the page immediately gets loaded when I reconnect to the net.
Steve
done. I want to follow up on my question, so I posted myself an answer. or how can I refine my question here on stackoverflow?
Steve
You can edit your question or ask a new one.
SLaks
A: 

thanks to SLaks, jQuery retries to reload now.

however, if I pull the internet cable off the PC, it still clears the screen and doesnt trigger the error. here's my new attempt:

$.ajax({
        url:'index.php',
        type:'HEAD',
        success: function() { location.reload(true); },
        error: function() {$('DIV').append('<p>ERROR</p>');}
        })
Steve
Add a random number to the URL to avoid caching (`url: 'index.php' + Math.random()`)
SLaks
Also, install [Firebug](http://getfirebug.com), replace `location.reload` with `console.log(arguments)`, and check what happens on the Console tab as you unplug the connection.
SLaks
cool, thanks, I like the approach.. BTW: I use opera dragonfly;)
Steve