I'm trying to dynamically create a html list that update when new information arrives. Here is my code :
setlast();
function setlast() {
last = [
"xyyxx", "yxyxx", "xxxxy", "yyxxy", "yxyxy"]
};
setInterval(function() {
last[0] = Math.random();
last[1] = Math.random();
last[2] = Math.random();
last[3] = Math.random();
last[4] = Math.random();
}, 2000);
createHtml();
function createHtml() {
setTimeout(function(){
ul = document.createElement("ul");
document.body.appendChild(ul);
ul.setAttribute("id", "lyl");
for(w=0; w<5; w++) {
li = document.createElement("li");
ul.appendChild(li);
}
updateT();
},3500);
}
function updateT() {
setInterval(function() {
li.innerHTML = last[w];
},2000);
}
#listwrapper {
margin-top: 2%;
width : 100%;
}
li {
display : inline;
float : left;
padding : 10px;
}
Doing like above, I get undefined.
If I do it like below, the code works but each li do not update every time Math.random generates a new number.:
setlast();
function setlast() {
last = [
"xyyxx", "yxyxx", "xxxxy", "yyxxy", "yxyxy"]
};
setInterval(function() {
last[0] = Math.random();
last[1] = Math.random();
last[2] = Math.random();
last[3] = Math.random();
last[4] = Math.random();
}, 2000)
createHtml();
function createHtml() {
setTimeout(function(){
ul = document.createElement("ul");
document.body.appendChild(ul);
ul.setAttribute("id", "lyl");
for(w=0; w<5; w++) {
li = document.createElement("li");
ul.appendChild(li);
li.innerHTML = last[w];
}
},3500)
}
#listwrapper {
margin-top: 2%;
width : 100%;
}
li {
display : inline;
float : left;
padding : 10px;
}
So my question is: how can I get the list to display and update every time a new number is generated ?
Thanks a lot.