1

我试图通过让它循环遍历字符串数组并在 3 秒延迟后显示每个字符串来测试 javascript 中的计时函数,但是当我调用该函数时,它会执行一种奇怪的模式,即只取第一个字符串的第一个字母,然后第二个字符串的第二个字母,依此类推...如果数组中的第四个项目没有第四个字母,则打印未定义。请帮忙。谢谢

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

4 回答 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);
 }
})()
于 2013-03-22T18:51:18.383 回答
1

而不是document.write(name[i]);,做document.write(name);

于 2013-03-22T18:43:24.133 回答
0

一探究竟:

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)}
于 2013-03-22T18:49:10.617 回答
0

您的document.write(name[i]);

应该

document.write(name);

谢谢

于 2013-03-22T18:45:04.733 回答