开发者

How can I refresh a page via jQuery and ensure that there's a connection?

开发者 https://www.devze.com 2022-12-29 23:40 出处:网络
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.

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 开发者_如何学Pythonthe 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.


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); }


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>');}
        })
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号