简体   繁体   English

在每次迭代中延迟地迭代对象属性

[英]Iterating over object properties with delay in each iteration

This is my code: 这是我的代码:

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();
    }
}       
}

I am iterating over all parts and trying to set their opacity=1. 我遍历所有parts并尝试将其opacity=1.设置opacity=1.

So these commands: 所以这些命令:

maleBackNoZoom.parts[prop].opacity = 1;
paper.view.update();

should be called every n seconds 应该每n秒调用一次

How can I add a delay on each iteration? 如何在每次迭代中添加延迟?


Note: I have of course read through all the question for delaying iterations in javascript but most of them have to do with arrays, not object properties traversals 注意: 我当然已经阅读了所有有关延迟javascript中迭代的问题,但大多数问题与数组有关,而不是对象属性遍历

You can create a list of properties that you want to illuminate. 您可以创建要显示的属性列表。 Then handle the illumination just like you would for an array. 然后像处理阵列一样处理照明。

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM