Wasteland
Wasteland

Reputation: 5379

JavaScript - How do I display a text, character by character

I'm trying to display a text taken from an array, character by character using JavaScript. I have managed to do it with one part of the array. I can't find a way of going to a newline and displaying the rest.

var container = document.getElementById("container");

var notes = [
{scenario: 1, intro: "This is the introduction.", que: "What is the weight of ....?"},
{scenario: 2, intro: "This is the second scen.", que: "What is the second law of ...?"},
{scenario: 3, intro: "This is the third thing.", que: "What is the third law of ...?"},
];


function splTxt(txt) {
    // Split string into characters                                                                                                                                           
    t = txt.split('');
return t
}

function terminal(cl, i) {
// Create a div element and display message                                                                                                                               
var div = document.createElement('div');
div.className = cl;
container.appendChild(div);

// Take the first element of the array                                                                                                                                    
// and extract the intro string                                                                                                                                           
var txt = splTxt(notes[0].intro);
var i = 0;

// Display text, character by character                                                                                                                                   
var display = setInterval(function() {
    div.textContent += txt[i];
    if (i == (txt.length-1)) {
        clearInterval(display);
    }
    i += 1
}, 100);

}

terminal('blueTh', 0);

After it displayed notes[0].intro, I'd like it to go to a new line and display notes[0].que.

I have tried to do

var txt = splTxt(notes[0].intro + '<br />' +  notes[0].que);

But obviously, it just displays <br /> and prints both messages on the same line.

Upvotes: 2

Views: 3201

Answers (2)

Kaleem Nalband
Kaleem Nalband

Reputation: 697

message = document.getElementById("fly").innerHTML; // $ = taking a new line
distance = 150; // pixel(s)
speed = 20; // milliseconds

var txt="",
	num=0,
	num4=0,
	flyofle="",
	flyofwi="",
	flyofto="",
	fly=document.getElementById("fly");


function stfly() {
	for(i=0;i != message.length;i++) {
		if(message.charAt(i) != "$")
			txt += "<span style='position:relative;visibility:hidden;' id='n"+i+"'>"+message.charAt(i)+"<\/span>";
		else
			txt += "<br>";
	}
	fly.innerHTML = txt;
	txt = "";
	flyofle = fly.offsetLeft;
	flyofwi = fly.offsetWidth;
	flyofto = fly.offsetTop;
	fly2b();
}

function fly2b() {
	if(num4 != message.length) {
		if(message.charAt(num4) != "$") {
			var then = document.getElementById("n" + num4);
			then.style.left = flyofle - then.offsetLeft + flyofwi / 2;
			then.style.top = flyofto - then.offsetTop + distance;
			fly3(then.id, parseInt(then.style.left), parseInt(then.style.left) / 5, parseInt(then.style.top), parseInt(then.style.top) / 5);
		}
		num4++;
		setTimeout("fly2b()", speed);
	}
}

function fly3(target,lef2,num2,top2,num3) {
	if((Math.floor(top2) != 0 && Math.floor(top2) != -1) || (Math.floor(lef2) != 0 && Math.floor(lef2) != -1)) {
		if(lef2 >= 0)
			lef2 -= num2;
		else
			lef2 += num2 * -1;
		if(Math.floor(lef2) != -1) {
			document.getElementById(target).style.visibility = "visible";
			document.getElementById(target).style.left = Math.floor(lef2);
		} else {
			document.getElementById(target).style.visibility = "visible";
			document.getElementById(target).style.left = Math.floor(lef2 + 1);
		}
		if(lef2 >= 0)
			top2 -= num3
		else
			top2 += num3 * -1;
		if(Math.floor(top2) != -1)
			document.getElementById(target).style.top = Math.floor(top2);
		else
			document.getElementById(target).style.top = Math.floor(top2 + 1);
		setTimeout("fly3('"+target+"',"+lef2+","+num2+","+top2+","+num3+")",25)
	}
}

stfly()
<h4 id='fly'>This is dummy text for demo purpose</h4>

Upvotes: 1

Oriol
Oriol

Reputation: 288100

You have two options:

  • Insert <br /> and tell the browser to parse it as HTML.

    You can do this by using the innerHTML property instead of textContent.

    This will allow you to use HTML things like <br />, but you will have to escape &, <, > when they are supposed to be plain text. Don't do this if you don't trust the text.

    var container = document.getElementById("container");
    var notes = [
      {scenario: 1, intro: "This is the introduction.", que: "What is the weight of ....?"},
      {scenario: 2, intro: "This is the second scen.", que: "What is the second law of ...?"},
      {scenario: 3, intro: "This is the third thing.", que: "What is the third law of ...?"}
    ];
    function terminal(cl, i) {
      var div = document.createElement('div');
      div.className = cl;
      container.appendChild(div);
      var txt = [notes[0].intro, notes[0].que].join('\n').split('');
      var i = 0;
      (function display() {
        if(i < txt.length) {
          div.innerHTML += txt[i].replace('\n', '<br />');
          ++i;
          setTimeout(display, 100);
        }
      })();
    }
    terminal('blueTh', 0);
    <div id="container"></div>

  • Insert a newline character and tell the browser to display it properly.

    In HTML, whitespace characters collapse by default. You can change this behavior by setting the white-space CSS property to pre, pre-wrap or pre-line. For example, white-space: pre preserves all whitespace and doesn't wrap text.

    var container = document.getElementById("container");
    var notes = [
      {scenario: 1, intro: "This is the introduction.", que: "What is the weight of ....?"},
      {scenario: 2, intro: "This is the second scen.", que: "What is the second law of ...?"},
      {scenario: 3, intro: "This is the third thing.", que: "What is the third law of ...?"}
    ];
    function terminal(cl, i) {
      var div = document.createElement('div');
      div.className = cl;
      container.appendChild(div);
      var txt = [notes[0].intro, notes[0].que].join('\n').split('');
      var i = 0;
      (function display() {
        if(i < txt.length) {
          div.textContent += txt[i];
          ++i;
          setTimeout(display, 100);
        }
      })();
    }
    terminal('blueTh', 0);
    #container {
      white-space: pre-line;
    }
    <div id="container"></div>

Upvotes: 6

Related Questions