How Do You Display Text Word By Word For Text That Is Received Via Ajax?
I'm trying to display text word by word or letter by letter via javascript/jQuery. I've found questions answering that already. The difference is, this text is being delivered via
Solution 1:
var textToDisplay = "Even more awesome text inside here, but displayed one word at a time",
$output = $("p");
$("button").click(function() {
var displayInt;
textToDisplay = textToDisplay.split(' ');
$output.empty();
displayInt = setInterval(function() {
var word = textToDisplay.shift();
if (word == null) { returnclearInterval(displayInt); }
$output.append(word + ' ');
}, 300);
});
<scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><p> Some awesome text inside here </p><button>Load</button>
Solution 2:
Try this, in your javascript file:
var returnedString = "Even more awesome text inside here, but displayed one word at a time"; //this is your returned stringvar stringToArray = returnedString.split(" ");
for(var i = 0; i < stringToArray.length; i++)
{
$("#containerElement").append(stringToArray[i] + "<br/>");
}
You can also use setTimeout to control how soon the next word is displayed.
Post a Comment for "How Do You Display Text Word By Word For Text That Is Received Via Ajax?"