JavaScript: I am trying to use a setInterval() function to generate one message every 3 seconds -
currently running setinterval() funnction populate div new messages , attempting populate 1 @ time, current setinterval() generating multiple @ time. how can fix this?
here's code snippet:
function newmessage(){ // loop goes through emails , creats divs subject, date, sender, , body for(var = 0; < window.geemails.length; i++ ) { //email container var div = document.createelement("div"); //date var datefield = document.createelement("h1"); datefield.classname = "date"; datefield.innerhtml = window.geemails[i].date; div.appendchild(datefield); //subject var subjectfield = document.createelement("h1"); subjectfield.classname = "subject"; subjectfield.innerhtml = window.geemails[i].subject; div.appendchild(subjectfield); //sender var senderfield = document.createelement("h1"); senderfield.classname = "sender"; senderfield.innerhtml = window.geemails[i].sender; div.appendchild(senderfield); //body var bodyfield = document.createelement("p"); bodyfield.classname = "body"; bodyfield.innerhtml = window.geemails[i].body; div.appendchild(bodyfield); //separate message content containers document.getelementbyid("main").appendchild(div); //inbox counter document.getelementbyid('counter').innerhtml = "you have " + window.geemails.length + " messages"; } } newmessage(); //interval loading new messages function addnewmessage() { setinterval(function(){window.geemails.push(getnewmessage());newmessage();}, 3000); } addnewmessage();
//interval loading new messages function addnewmessage() { setinterval(function () { window.geemails.push(getnewmessage()); newmessage(); }, 3000); }
settimeout happen once. setinterval occur every x milliseconds.
update problem code calling addnewmessage inside interval function. don't need when call setinterval.
Comments
Post a Comment