1

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();

4 Answers 4

4

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>

Sign up to request clarification or add additional context in comments.

8 Comments

Further: would it be possible to then run this through a loop to run myFunction(); 10x?
@samseurynck, you're welcome. I'm glad it works. I don't understand the what you're trying to achieve further. Can you please explain a bit more or make an jsfiddle.net example based on what you have now?
I would like to run the function, myFunction(), multiple times until the text from the array fills up the entire window. I have been guessing that the best way to do that is to run the function through a loop, so that the "one" "two" "three" "four" array is appended over and over again, with delay from myFunction() intact. Does that make more sense?
So far I have this, which isn't exactly what I want since the array is not appending in order like it was before. jsfiddle.net/samseurynck/yk06Lv0g/1
Ah, that's it! Thank you again.
|
0

Use the index to increment the delay timer for setTimeout()

var arr = ["one ", "two ", "three ", "four ", "five "];

function myFunction() {
  var $box = $(".testBox"),// cache container to avoid dom search each iteration
    delay = 500; //time in ms

  jQuery.each(arr, function(index, value) {
    setTimeout(function() {
      $box.append('<p class="box">' + value + '</p>');
      console.log('yes');
    }, index * delay)

  });
}

myFunction();

fiddle demo

Comments

0

Just use the index for the timeout. Example with 1 second in between:

var arr = ["one ", "two ", "three ", "four ", "five "];

function myFunction() {
  jQuery.each(arr, function(index, value) {
    setTimeout(function(){
        $(".testBox").append('<p class="box">' + value + '</p>');
        console.log('yes');
    }, index*1000)
  });
}

myFunction();

Comments

0

Without adding a new variable to count the iteration, just use the index param

var arr = ["one ", "two ", "three ", "four ", "five "];

function myFunction() {
 jQuery.each(function(index, value) {
    setTimeout(function() {
      $(".testBox").append('<p class="box">' + value + '</p>');
      console.log('yes');
    }, 1000 * index)
  });
}

myFunction();

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.