jquery.each在继续循环之前等待函数完成
jquery .each waiting for function to finish before continuing through loop
我本质上是在尝试循环使用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);
}
});
}
使延迟累积: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');
});
我将反转工作:
- 你从你想写的字符串开始
-
对于每个字符串:
- 查找相应的列表项(按id)
- 用打字机的风格写出整串
-
完成后调用下一次迭代。
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); }
演示
简化一下如何?使用两个变量在一个循环中迭代稍微修改过的数据结构的全部内容。就像这样。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);
}
}
相关文章:
- JavaScript循环并等待函数
- 等待函数执行后再继续
- 嵌套的异步/等待函数
- 等待函数完成,然后将返回值分配给变量JS
- 如何在Node.js中等待函数完成后再继续
- 如何在JavaScript中创建一个等待函数,允许您在后台运行音频等内容
- 等待函数结束(在 async.series 中)无法按预期工作
- 等待函数 $.get 中的代码完成以继续
- Javascript等待函数结束,包括来自node.js的异步MYSQL查询
- 并行调用异步/等待函数
- Javascript 睡眠/延迟/等待函数
- 如何在 Node.js 中等待函数完成
- Javascript - 等待函数完成
- Javascript for loop不等待函数响应
- 等待函数完成对其他函数的调用
- 循环的Javascript,等待函数回调
- 如何使angular在继续之前等待函数返回值
- 如何让html在AngularJS控制器中等待函数
- Node.js强制等待函数完成
- azure异步javascript后端等待函数