[RESOLVED] Reload div that is populated by JS

I have the following JS file.

[code]$.post( “trains.php”, { to: “LIV”, from: “WKI” })
.done(function( data ) {
obj = JSON.parse(data);

var i = 0;
var len = obj.length;
var text2 = “”;

for (; i < len; ) {
text2 += " " + obj[i][1] + " " + obj[i][3] + “
”;
i++;
}
document.getElementById(“trains”).innerHTML = "Trains to " + obj[0][2] + “
” + text2;
});
[/code]

this populates a DIV called trains.

how can i reload just that one DIV every hour with out a refresh of the page.

Using the Meta Tag to refresh:

<meta http-equiv="refresh" content="3600">

Or Javascript:

function ajax(){

//your code

setTimeout(ajax(), 3600);
}

ajax();

Hate to be pedantic, but doesn’t this refresh the page? :smile:

V/r,

:slight_smile:

hehe it does but sometimes it’s enough

nice one i’ve done the following:

function trains(){
$.post( "trains.php", { to: "LIV", from: "WKI" })
  .done(function( data ) {
    obj = JSON.parse(data);
  


var i = 0;
var len = obj.length;
var text2 = "";

for (; i < len; ) {
    text2 += "<img src='./images/whitetrain.png' id='train-icon'>&nbsp;" + obj[i][1] + "&nbsp;" + obj[i][3] + "<br>";
    i++;
}
document.getElementById("trains").innerHTML = "Trains to " + obj[0][2] + "<Br>" + text2;
});
}
setTimeout(trains(), 3600);

should hopefully do the job :slight_smile:

another quick one guys how would i add for every iteration of the train time a opacity setting so each one gets faded and faded

i am going to change

text2 += "<img src='./images/whitetrain.png' id='train-icon'>&nbsp;" + obj[i][1] + "&nbsp;" + obj[i][3] + "

to something like

text2 += "<div style="opacity: 1;"<img src='./images/whitetrain.png' id='train-icon'>&nbsp;" + obj[i][1] + "&nbsp;" + obj[i][3] + " "</div>"

all done :slight_smile: this can be closed :slight_smile:

We tend to not close threads in case others have something useful to add, but I’ve updated the thread title to indicate that this issue is now resolved.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.