I'd like to run this array through the jQuery.each function and add a delay in-between each iteration, creating a pause in between each word appending to the div. I've seen other similar questions answered using setTimeout but I have been unsuccessful applying that to my code.
https://jsfiddle.net/samseurynck/7xw9ujjh/2/
var arr = ["one ", "two ", "three ", "four ", "five "];
function myFunction() {
jQuery.each(arr, function(index, value) {
$(".testBox").append('<p class="box">' + value + '</p>');
console.log('yes');
});
}
myFunction();
You can create a counter and use setTimeout()
method:
var arr = ["one ", "two ", "three ", "four ", "five "];
function myFunction() {
var count = 0;
jQuery.each(arr, function(index, value) {
setTimeout(function() {
$(".testBox").append('<p class="box">' + value + '</p>');
console.log('yes');
}, count * 1000)
count++;
});
}
myFunction();
.testbox {
height: 100%;
width: 100%;
position: absolute;
}
p {
display: inline-block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="testBox">
<p class="box">
</p>
</div>