Add delay on each loop iteration

user3818143

The overall goal of this is to achieve a typing-like animation for some text. I'm looping through each letter of a string and adding it to the DOM with delays between each letter, using setTimeout within a FOR loop.

My HTML--

<h1 id="name">Hello World!</h1>

My Javascript--

$(document).ready(function(){
    var typeText = $("#name").html();
    var textLength = typeText.length;
    $("#name").empty();

    for(i=0;i<textLength;i++) {
        (function(i){
            console.log("first function being fired!");
            setTimeout(function(i){
                console.log("setTimeout function being fired!");
                console.log(i);
                var letter = typeText.substring(i,i+1);
                console.log(letter);
                $("#name").append(letter);
            },5000);
        })();
    }
});

I don't get any errors, but I am logged first with 12 accounts of "first function being fired!", a 5 second delay, then 12 accounts of this:

setTimeout function being fired<br />
undefined<br />
[line-break]

I think I'm missing a fundamental part of FOR loops, or don't completely understand how functions are handled within them.

Scimonster

You're not passing a value to your inner is.

$(document).ready(function(){
    var typeText = $("#name").html();
    var textLength = typeText.length;
    $("#name").empty();

    for(i=0;i<textLength;i++) {
        (function(i){
            console.log("first function being fired!");
            setTimeout(function(){ // not necessary here
                console.log("setTimeout function being fired!");
                console.log(i);
                var letter = typeText.substring(i,i+1);
                console.log(letter);
                $("#name").append(letter);
            },5000);
        })(i); // pass here
    }
});

And if you want each to fire 5 seconds after the previous, instead of just waiting 5 seconds and doing all of them, you can set the timeout like that:

setTimeout(function(){
// ...
}, 5000 * i);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

add a short delay between each iteration of do while loop

From Dev

add a short delay between each iteration of do while loop

From Dev

How can I add a delay inside each iteration of an _.each loop in underscore.js?

From Dev

How can I add a delay inside each iteration of an _.each loop in underscore.js?

From Dev

Delay each loop iteration in node js, async

From Dev

Delay between each iteration of foreach loop?

From Dev

add results of a for loop after each iteration

From Dev

For loop delay before next iteration

From Dev

In Markdown PDF, how to add page break after each iteration of for loop?

From Dev

How to add elements to an array in Bash on each iteration of a loop?

From Dev

Use a while loop in java to add to a different variable in each iteration

From Dev

PHP add key value to array with each loop iteration

From Dev

Add xsl:param only on first iteration of the xsl:for-each loop

From Dev

How to add 300 to a variable on each iteration using a for loop in php?

From Dev

How to add data after each iteration of for loop to the app view in Ionic?

From Dev

await task in each iteration of a loop

From Dev

Delaying For Loop Between Each Iteration

From Dev

Delay for-loop each run

From Dev

Applying delay between each iteration of jQuery's .each() method

From Dev

For Loop Iteration Happening all at Once Rather than By Delay

From Dev

How to do a delay after every iteration in a foreach loop?

From Dev

Storing Each Iteration of Array after Loop Completion

From Dev

jQuery skip an iteration of the parent loop in nested .each

From Dev

Loop 'n' elements in groups in each iteration

From Dev

Can I print immediately for each iteration in a loop?

From Dev

Refactoring for loop with each iteration setting a different property

From Dev

Canvas not updating after each loop iteration

From Dev

Why an each loop in a Jenkinsfile stops at first iteration

From Dev

Does for loop parameters execute each iteration?

Related Related

  1. 1

    add a short delay between each iteration of do while loop

  2. 2

    add a short delay between each iteration of do while loop

  3. 3

    How can I add a delay inside each iteration of an _.each loop in underscore.js?

  4. 4

    How can I add a delay inside each iteration of an _.each loop in underscore.js?

  5. 5

    Delay each loop iteration in node js, async

  6. 6

    Delay between each iteration of foreach loop?

  7. 7

    add results of a for loop after each iteration

  8. 8

    For loop delay before next iteration

  9. 9

    In Markdown PDF, how to add page break after each iteration of for loop?

  10. 10

    How to add elements to an array in Bash on each iteration of a loop?

  11. 11

    Use a while loop in java to add to a different variable in each iteration

  12. 12

    PHP add key value to array with each loop iteration

  13. 13

    Add xsl:param only on first iteration of the xsl:for-each loop

  14. 14

    How to add 300 to a variable on each iteration using a for loop in php?

  15. 15

    How to add data after each iteration of for loop to the app view in Ionic?

  16. 16

    await task in each iteration of a loop

  17. 17

    Delaying For Loop Between Each Iteration

  18. 18

    Delay for-loop each run

  19. 19

    Applying delay between each iteration of jQuery's .each() method

  20. 20

    For Loop Iteration Happening all at Once Rather than By Delay

  21. 21

    How to do a delay after every iteration in a foreach loop?

  22. 22

    Storing Each Iteration of Array after Loop Completion

  23. 23

    jQuery skip an iteration of the parent loop in nested .each

  24. 24

    Loop 'n' elements in groups in each iteration

  25. 25

    Can I print immediately for each iteration in a loop?

  26. 26

    Refactoring for loop with each iteration setting a different property

  27. 27

    Canvas not updating after each loop iteration

  28. 28

    Why an each loop in a Jenkinsfile stops at first iteration

  29. 29

    Does for loop parameters execute each iteration?

HotTag

Archive