I want to replace the content of a paragraph using items from an array dynamically time after time. The output is fine when I use console.log()
to check the results. But it is not replacing the content on the paragraph as expected, just shows the last word when the iteration is complete.
Here is the code I made to create and iterate over the array:
$(document).ready(function()
{
var _strng = "Lorem ipsum dolor sit amet";
var _array = new Array();
_array = _strng.split(' ');
jQuery.each(_array, function(index,item)
{
console.log(item); // Works fine
$('p').html(item); // Only shows the last word when the iteration is over
wait(1000); // Custom function
console.clear();
});
});
The wait() function:
function wait(_timeframe)
{
var final = 0;
var timeframe = new Date(_timeframe);
var initial = Date.now();
final = initial + _timeframe;
while (Date.now() < final) { };
}
HTML code:
<p>Text to be replaced here</p>