这是我的代码:
maleBackNoZoom = {
parts:{
armsRight: findItemById("29") ,
armsLeft: findItemById("28"),
legsRight: findItemById("21"),
legsLeft: findItemById("22"),
back: findItemById("24"),
buttocks: findItemById("23"),
neck: findItemById("26"),
head: findItemById("27")
},
illuminate: function() {
for (prop in maleBackNoZoom.parts) {
maleBackNoZoom.parts[prop].opacity = 1;
paper.view.update();
}
}
}
我正在迭代所有parts
并尝试设置他们的opacity=1.
所以这些命令:
maleBackNoZoom.parts[prop].opacity = 1;
paper.view.update();
应该每隔n秒调用
如何在每次迭代时添加延迟?
注意: 我当然已经阅读了延迟javascript迭代的所有问题但其中大多数都与数组有关,而不是对象属性遍历
答案 0 :(得分:1)
您可以创建要照亮的属性列表。然后就像对阵列一样处理照明。
illuminate: function() {
var propertiesToIlluminate = [], prop, illuminateInternal, i = 0, delay = 1000, intervalId;
for (prop in maleBackNoZoom.parts) {
propertiesToIlluminate.push(prop);
}
illuminateInternal = function () {
var property = propertiesToIlluminate[i];
maleBackNoZoom.parts[property].opacity = 1;
paper.view.update();
i++;
if (i === propertiesToIlluminate.length) {
clearInterval(intervalId);
}
};
intervalId = setInterval(illuminateInternal, delay);
}