1

i am trying to test the timing function in javascript by having it loop through an array of strings and displaying each after a 3 second delay but when i call the function it does a weird pattern of only taking the first letter of the first string, then the second letter of the second string, and so on... if, say the fourth item in the array has no fourth letter it prints undefined. please help. thanks

function myFunc () {
    setTimeout(function () {
        var contacts = {
            addressBook : [
            {
                'name' : 'Jane',
                'email' : 'JaneDoegmail.com'
            },
            {
                'name' : 'Meggie',
                'email' : 'meggiegmail.com'
            },
            {
                'name' : 'John',
                'email' : 'johnDoegmail.com'
            },
            {
                'name' : 'Paul',
                'email' : 'paulgmail.com'
            },
            {
                'name' : 'Bo',
                'email' : 'bogmail.com'
            }
            ]
        };
        var object = contacts.addressBook;
        var i;
        for (var i = 0; i < object.length; i++) {
            var item = object[i];
            var name = item.name;
            var email = item.email;

            document.write(name[i]);
        };

        if (i < 10) {
            myFunc();
        };
    }, 3000)
}
myFunc();

4 Answers 4

2

Here is a working jsFiddle demo

Basically, your output is correct from what you tell it to do, it is showing you the character at the index of the name string. You should use (name) instead of (name[i]).

However, I think this is what you were probably after: suggested improvement demo

Basically, it allows the loop to be externally controller by a counter which will increment as called and stop once it has displayed all of the contact addresses.

js:

var c = document.getElementById("console");
var contacts = {
  addressBook : [
    {
        'name' : 'Jane',
        'email' : 'JaneDoegmail.com'
    },
    {
        'name' : 'Meggie',
        'email' : 'meggiegmail.com'
    },
    {
        'name' : 'John',
        'email' : 'johnDoegmail.com'
    },
    {
        'name' : 'Paul',
        'email' : 'paulgmail.com'
    },
    {
        'name' : 'Bo',
        'email' : 'bogmail.com'
    }
  ]
};
var i = 0;
(function myFunc () {
 var item = contacts.addressBook[i++];
 var name = item.name;
 var email = item.email;
 c.innerHTML += name +", " + email + "<br>";
 if (i < contacts.addressBook.length) {
  setTimeout(myFunc,3000);
 }
})()
Sign up to request clarification or add additional context in comments.

Comments

1

Instead of document.write(name[i]);, do document.write(name);

1 Comment

that worked thanks but it just outputs all the names. im trying to have it output one then pause then another and pause. any thoughts. much appreciated.
0

Your document.write(name[i]);

should be

document.write(name);

Thanks

1 Comment

Actually, it shouldn't. document.write is pretty bad.
0

Check it out:

myFunc();
function myFunc () {
setTimeout(function () {
    var contacts = {
    addressBook : [
        {
            'name' : 'Jane',
            'email' : 'JaneDoegmail.com'
        },
        {
            'name' : 'Meggie',
            'email' : 'meggiegmail.com'
        },
        {
            'name' : 'John',
            'email' : 'johnDoegmail.com'
        },
        {
            'name' : 'Paul',
            'email' : 'paulgmail.com'
        },
        {
            'name' : 'Bo',
            'email' : 'bogmail.com'
        }
    ]
};
var object = contacts.addressBook;
var i;
for (var i = 0; i < object.length; i++) {
    var item = object[i];
    var name = item.name;
    var email = item.email;

    document.write(name+" -->  ");
};

if (i < 10) {
    myFunc();
};
}, 3000)}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.