MOOcow102
MOOcow102

Reputation: 91

setTimeout Only Working for One Function

I have two tables that I want to refresh every so often. Both are using pretty much the same javascript, except that one works and the other doesn't.
Here is the first table's code (Only refreshes once):

function refreshTable() {
    $('#tablefill').load('table.php', function(){
       table1refr=setTimeout(refreshTable, 10000);
    $.each(selected, function(index, value) {
       document.getElementById(value).innerHTML = '-';
    });
    });
}

And here is the second (Refreshes every 10 seconds, as it should):

function refreshFileTable() {
    $('#filetablefill').load('filetable.php', function(){
       table2refr=setTimeout(refreshFileTable, 10000);
    });
}

I tried switching setTimeout to setInterval on both of them, which worked, but the browser tab would completely freeze after a minute or so of sitting on the page. If you could suggest a way to prevent that, I would love to use setInterval.

Upvotes: 1

Views: 126

Answers (1)

Rupesh Patel
Rupesh Patel

Reputation: 3065

If you want to use setInterval you don't need to set it every time, try this

$(document).ready(function(){
  setInterval(refreshFileTable,1000);
})

function refreshFileTable() {
    $('#filetablefill').load('filetable.php', function(){
       // do nothing or remove this handler entirely
       // if you were setting intervals over here you have created new interval in every 10 seconds         and thats how it hanged 
    });
}

Upvotes: 1

Related Questions