开发者

setTimeout not working correctly with jQuery and load()

i have a problem with my code i have 2 tables loaded into two divs like so:

loadTable($("#vbeTable"),'getUpdateA')
loadTable($("#vbcTable"),'getUpdateB')

I also have this:

$("#vbeTable, #vbcTable").live('mouseover mouseout', function(event) {
    if (event.type == 'mouseover') {
//        console.log('hovering over',$(this));
        $(this).attr('update',false)
    } else {
//        console.log('NOT hovering over',$(this));
        $(this).attr('update',true)
    }
})

and the loadTable function is like so:

开发者_如何学Python
function loadTable($table, $php, $noRefresh){
    if($table.attr('update') == 'false'){
        console.log('not updating', $table, $table.attr('update'))
        setTimeout( function () { loadTable($table, $php); }, 1000)
    }
    $table.load($php+'.php',function(response, status, xhr) {
        if (status == "success") {
            if(!$noRefresh){
                console.log('acquired table')
                setTimeout( function () { loadTable($table, $php); }, 1000)
            }
        }
        else {
            console.log('error aquiring lock on', $table.attr('id'), response, status, xhr)
        }
    });

}

*for some reason the setTimeout function is not waiting the right amount of time and in the console i get:

(96) liveLoads.js:35acquired table

(7711) liveLoads.js:29not updating [ <div id=​"vbcTable" update=​"true">​…​</div>​ ] false

(2)<exception> <exception> <exception> <exception>* orginally both vbe and vbc are empty divs.

Can enyone help me out here?

*UPDATE*

I also have this code:

function expand(EntID) {
    console.log('expand',EntID)
    $.ajax({
        url: "showRows.php?ID=" + EntID
    });
}

which when something on the table is clicked that calls this function it runs.

but then the other functions setTimeouts go into a tizzy and they get called a million times in a row without paying attention to the time


You're calling your timeout callback function instead of passing it to the timeout.

Instead of:

// this will call loadTable right away and setTimeout will trigger the returned value in a sec.
setTimeout( loadTable($table, $php), 1000)

Do:

// setTimeout will trigger the anon. function in a sec
setTimeout( function () { loadTable($table, $php); }, 1000)

You're also calling the same function, loadTable, from within loadTable. And I'm not seeing a break in this endless loop.


This part looks wrong:

if (status == "success") {
    if(!$noRefresh){
        console.log('acquired table')
        setTimeout( loadTable($table, $php), 1000)
    }
}

In addition to the fact that this does not pass the correct type to setTimeout (param1 must be a function, you give it the return result of the function call to loadTable) - I don't think you want it to call loadTable ANYWAY. Instead call a function that parses the data and populates your html elements! So:

if (status == "success") {
    if(!$noRefresh){
        console.log('acquired table')
        setTimeout(function () {drawMyTableWithThisData(someTable, data);}, 1000);
    }
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜