jquery .ee等待函数在继续循环之前完成

时间:2013-02-13 01:52:59

标签: javascript jquery

我实际上是在尝试遍历LI标签的集合并插入一些文本来模拟某人编写要做的事情列表的外观。它可以工作,但它会同时写入每个列表项而不是等待。有没有一种简单的方法来实现这一目标?我在这里有一个JS小提琴设置:http://jsfiddle.net/fZpzT/但代码看起来像这样。感谢。

function addListItems() {
var str = {
    listitem1:'personal background check',
    listitem2:'look into my sketchy neighbor',
    listitem3:'look up my driving record',
    listitem4:'pick up milk',
    listitem5:'wash the car'
}

$('.list-container li').each(function(){
    var z = $(this).attr('id');
    var str2 = str[z];
    var delay = 0;
    for (var i = 0; i <= str2.length; i++) {
        (function(str2){
            delay += 100 + Math.floor(Math.random()*11)*6;
            setTimeout(function(){
                appendStr(str2);       
            },delay);
        })(str2[i])
    }
    function appendStr(str2) {
      $('#'+ z).append(str2);
    }
});
}

3 个答案:

答案 0 :(得分:4)

延迟累计:Demo on jsFiddle

var str = {
    listitem1: 'write the first item',
    listitem2: 'write the second item',
    listitem3: 'write the third item',
    listitem4: 'write the fourth item',
    listitem5: 'write the fifth item'
}, cumulativeDelay = 0;

$('.list-container li').each(function () {
    var z = $(this).attr('id');
    var str2 = str[z];
    var delay = cumulativeDelay;
    for (var i = 0; i <= str2.length; i++) {
        (function (str2) {
            delay += 100 + Math.floor(Math.random() * 11) * 6;
            setTimeout(function () {
                appendStr(str2);
            }, delay);
        })(str2[i])
    }
    cumulativeDelay = delay;
    function appendStr(str2) {
        $('#' + z).append(str2);
    }
    $(this).css('color', 'red');
});

答案 1 :(得分:2)

我会改变工作方式:

  1. 您从要编写的字符串开始,
  2. 对于每个字符串:

    1. 找到相应的列表项(按ID)
    2. 以打字机样式写出整个字符串
    3. 完成后调用下一次迭代。

      var strings = [
        'personal background check',
        'look into my sketchy neighbor',
        'look up my driving record',
        'pick up milk',
        'wash the car'
      ];
      
      function iterate(strings, idx)
      {
        if (idx >= strings.length) { return; }
      
        var id = 'listitem' + (idx + 1),
        el = document.getElementById(id);
      
        typewriter(el, strings[idx], 0, function() {
          iterate(strings, idx + 1);
        });
      }
      
      function typewriter(el, str, idx, cb)
      {
        if (idx >= str.length) {
          return cb();
        }
      
        setTimeout(function() {
          el.innerText = str.substr(0, idx + 1);
          typewriter(el, str, idx + 1, cb);
        }, 100 + Math.floor(Math.random() * 11) * 6);
      }
      
  3. Demo

答案 2 :(得分:1)

简化怎么样?使用两个变量在单个循环中迭代略微修改的数据结构的整个内容。像这样。 http://jsfiddle.net/cXykd/

var strings = 
[
    { "id":"listitem1", "text": "write the first item" },
    { "id":"listitem2", "text": "write the second item" },
    { "id":"listitem3", "text": "write the third item" },
    { "id":"listitem4", "text": "write the fourth item" },
    { "id":"listitem5", "text": "write the fifth item" },
 ]

var stringsIndex = 0;
var textIndex = 0;

AddString();

function AddString(){
    if (stringsIndex < strings.length){
        if (textIndex >= strings[stringsIndex].text.length)
        {
            stringsIndex++;
            if (stringsIndex == strings.length)
            {
                return;
            }
            textIndex = 0;
        }

        $("#" + strings[stringsIndex].id).append(strings[stringsIndex].text[textIndex]);
        textIndex++;

        var delay = 10 + Math.floor(Math.random()*11)*6;
        setTimeout(AddString, delay);
    }
}
相关问题