【发布时间】:2014-08-23 23:42:06
【问题描述】:
这个让我头疼!
我有一个物品清单:
var prev = 'sally.charlie.ted';
var next = 'tina.heather.david';
然后我想向前和向后循环遍历它们,每个延迟 1 秒:
animate(prev, function () {
animate(next, function () {
console.log('done');
}, false);
}, true);
但现在我让自己感到困惑。也许这些是更好的方法?
function animate(url, callback, reverse, index, length) {
var items = url.split('.'),
total = items.length - 1;
if (typeof index === 'undefined') { index = reverse ? items.length - 1 : 0; }
if (typeof length === 'undefined') { length = reverse ? url.length + items[index].length + 1 : 0; }
if (index > total || index < 0) {
callback();
} else {
if (reverse === true) {
length -= items[index].length + (items.length - index);
} else {
length += items[index].length + index;
}
console.log(url.length, total, length, items[index], url.slice(0, length));
window.setTimeout(function () {
animate(url, callback, reverse, reverse ? index - 1 : index + 1, length);
}, 1000);
}
}
我想为每个项目获取正确的完整路径,无论是向前还是向后。我想要的向后输出是:
ted = sally.charlie.ted
charlie = sally.charlie
sally = sally
然后转发:
tina = tina
heather = tina.heather
david = tina.heather.david
这是一个有效的小提琴:
【问题讨论】:
标签: javascript loops animation settimeout