views:

157

answers:

5

What I want to do is that: a webpage with continuously updating content. (In my case is updating every 2s) New content is appended to the old one instead of overwriting.

Here is the code I have:

var msg_list = new Array(
    "<message>Hello, Clare</message>", "<message>Hello,Lily</message>",
    "<message>Hello, Kevin</message>", "<message>Hello, Bill</message>"
);
var number = 0;
function send_msg()
{
 document.write(number + " " + msg_list[number%4]+'<br/>');
 number = number + 1;
}
var my_interval = setInterval('send_msg()', 2000); 

However, in both IE and Firefox, only one line is printed out, and the page will not be updated anymore. Interestingly in Chrome, the lines are being printed out continuously, which is what I am looking for.

I know that document.write() is called when the page is loaded according to this link. So it's definitely not the way to update the webpage continuously. What will be the best way to achieve what I want to do?

Totally newbie in Javascript. Thank you.

Lily

A: 

The answers to this question may be helpful: http://stackoverflow.com/questions/2680129/whats-a-simple-way-to-web-ify-my-command-line-daemon

dreeves
+1  A: 

I would start by looking at the jQuery library. This will save you a lot of pain.

What you want to do is keep inserted lines into a table, using eg:

$('table tbody').append('<tr><td>some value</td></tr>');
James Westgate
He mentioned he is a total noob in javascript. Better for him to learn it using just nilla javascript and then learn how to do it in jQuery.
Bob
I disagree. Why go through all that pain if you dont have to. Its like saying learn assembler before learning c.
James Westgate
I disagree. It's like saying learn assembler before learning Java.
Daniel
Oh, come on guys - plain Javascript is *nothing* like Assembler - and rudimentary understanding of javascript is an essential basis for proper jQuery usage.
Már Örlygsson
My point still stands. Why struggle with the different browser incompatibilities when you dont have to? Why do you think it has taken javascript 15 years to take off as a popular solution?
James Westgate
@Bob, it's a "she"~~~ :-)
Lily
@James, I think I will start implementing using a JQuery version after pure JS version anyway~ :-)
Lily
@James, maybe because javascript was rife with vulnerabilities and used only for popups in the beginning? It certainly has matured as a language in recent years. It's only become popular because libraries like jQuery make it possible for any noob to claim they "know" javascript. As Már said, javascript is nowhere near as complicated as assembler. I would say a better comparison is that jQuery is like the crutch old guys use to walk, but you don't see babies using crutches to learn to walk, do you?@Lily, sorry about that!
Bob
jQuery isnt a crutch. Its an abstraction, it saves time. It makes your implementation future proof. It delivers more stable code, more quickly. Frameworks like jQuery helps brings programmers into client side RIA development, without it people wouldn't bother. In time they pick up the nuances of the JavaScript language.
James Westgate
+1  A: 

I would have a div or some other container, like this:

<div id="msgDiv"></div>

Then write to it like using .innerHTML, like this:

var msg_list = new Array(
    "<message>Hello, Clare</message>", "<message>Hello,Lily</message>",
    "<message>Hello, Kevin</message>", "<message>Hello, Bill</message>"
);
var number = 0;
function send_msg()
{
    document.getElementById("msgDiv").innerHTML += number + " " + msg_list[number%4]+'<br/>';
    number++;
}
var my_interval = setInterval(send_msg, 2000); 

You can see a working example of this here

Nick Craver
@Nick, I thought about your solution before, but the thing is only one line will be displayed, and new one will be overwriting the old one. What I want to do is to print out all the lines available~
Lily
@Lily - That isn't the case...try the example, you use `+=` instead of just `=` and it appends.
Nick Craver
@Nick, you are right! I missed the += when I first looked at your solution. Sorry for my carelessness! ;-)
Lily
+1  A: 

You can append to the innerHTML property:

var number = 0;
function send_msg()
{
    document.getElementById('console').innerHTML += (number + " " + msg_list[number%4]+'<br/>');
    number = number + 1;
}

This code will append the message to an element with an id of console, such as

<div id="console"></div>

By the way, it is bad practice to call setInterval with a string.

Instead, pass the function itself, like this:

var my_interval = setInterval(send_msg, 2000); 
SLaks
Extra `()` in the interval call there :)
Nick Craver
Fixed; thanks..
SLaks
Also worth pointing out that `new Array('a','b','c')` is sort of the n00b way of defining arrays -- `var msg_list = ['a','b','c'];` is much neater. :-)
Már Örlygsson
@Mar, you can tell I am 100% newbie from the very first sight~ haha~ ;-) Thanks for the tip!
Lily
Very good explanation~ thx!
Lily
+1  A: 

This would be an excellent opportunity for you to learn a little DOM programming.

Using the DOM to update the page should result in less overhead than simply concatenating more HTML into it. Find the node you want to put the updates into, and do an appendChild on each subsequent addition.

Jason