简体   繁体   中英

why does my jQuery auto-refresh stop working if I add in a if statement?

window.onload = setupRefresh;
function setupRefresh() {
    setInterval("refreshBlock();", 1000);
}

function refreshBlock() {

    $('#activeItems').load("current_auctions.php");

}

the above code automatically refreshes the content of a div tag on my page every 1 second.

window.onload = setupRefresh;
function setupRefresh() {
    setInterval("refreshBlock();", 1000);
}

function refreshBlock() {
    if ($('#myAccount').html() == 'My Accout') {
        $('#activeItems').load("current_auctions.php");
    };
}

The above code, which only has the addition of an if statement, loads up the page once in the beginning, and then ceases to refresh. How can I fix this code?

Probably because that conditional is returning false.

if ($('#myAccount').html() == 'My Accout') {

Quite likely because of the typo in 'Accout', as @nnnnn says.

You can debug this quite easily in Chrome or Firefox by using a script terminal. In Firefox you could install Firebug or in Chrome just press Crtl+Shift+J. Either way get to a JavaScript command-line interface and enter:

$('#myAccount').html() == 'My Accout'

In your page. You'll see it return either true or false . If it's false you can then easily tweak that condition until it's true where you're expecting.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM