How might I add a 1000 ms delay in this loop? I checked this result and it appears that they created their own method based on a variable. I'm only looking to add a static pause time between each iteration of the invocation of the selector()
function.
(function doIt(){
$.each(personArray, function(index, person){
$(this).delay(1000 * index)selector(person.RPI)
});
});
EDIT: To add more to this question, I'm specifically struggling to find a way for my script to pause after invoking the function once per array item. Or:
- Reference array item 1
- Invoke function (array item 1)
- Pause for some time
- Reference Array item 2
- Invoke function (array item 2)
- etc.
I get the answers about pausing between iterations of the function as a whole, but I'm looking for a way to execute once per array item with a pause in between.
EDIT: after some more reading I've restructured my code as shown below:
var i= 0;
var interval = 3000;
(function iterate () {
if (personArray.length > i) {
$('#name').html((personArray[i].first))
var x = (personArray[i].RPI);
selector(x);
i++;
}
setTimeout(iterate, interval);
})();
Now... a more important question is, how would I restart the ENTIRE function at an interval, say 24 seconds? Can I wrap this inside another setTimeout function with a 24000 snd argument? Here's a fiddle for that:
function person(first, last, RPI) {
this.first = first;
this.last = last;
this.RPI = RPI;
}
var MW = new person('Mike', 'W', 112792);
var MT = new person('Matt', 'T', 157750);
var DR = new person('Derek', 'R', 165413);
var DD = new person('Darrell', 'D', 112794);
var AG = new person('Andrew', 'G', 137183);
var KR = new person('Kris', 'R', 157751);
var BJ = new person('Bill', 'J', 82517);
var personArray = new Array();
var personArray = [MW, MT, DR, DD, AG, KR, BJ];
var i= 0;
var interval = 3000;
(function iterate () {
if (personArray.length > i) {
var x = (personArray[i].RPI);
$('#name').html((personArray[i].first));
selector(x);
i++;
setTimeout(iterate, interval);
}
})();
function selector(x){
$('#name2').html("this is x:"+ x );
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script>
<p id="name"></p>
<p id="name2"></p>