setTimeout()如何访问聚合物元素的成员

时间:2015-10-08 14:08:28

标签: javascript polymer

我无法访问'成员'我的聚合物' class'

Polymer({

    is: 'sound-propagation',
    time1: 0,
    time2: 0,
    timeDifference: 0,
    timeCounter:0,
    speed: 5,
    distance: 5,
    frequency: [],
    amplitude: 15,
    deflection: [],
    animation: 0,
    timeout: 0,
    counter: 0,

    appletStart: function(context, firstStart) {        
        this.initializeCircles();
        this.changeFrequency(0.5);
        this.setAnimation();        
    },

    setAnimation: function(){       
        var self = this;
        var timeout = setTimeout(function(){ console.log("frequencybla: "+self.frequency[6]);}, 3000);  

        this.animation = setInterval(function(){            
            if(self.counter >= 20){             
                self.time2 = Date.now();
                self.timeDifference = (self.time2 - self.time1)/20000;
                self.time1 = Date.now();
                self.counter = 0;               
            }

            for(var i = 0; i <=19; i+=1){               
                self.deflection[i] = self.amplitude*Math.sin(self.timeCounter*self.frequency[i]*2*Math.PI);
                //console.log("deflection: "+self.deflection[i]);
                //console.log("amplitude: "+self.amplitude);
                //console.log("timeCounter: "+self.timeCounter);
                //console.log("frequency: "+self.frequency[i]);             
            }

            self.timeCounter += self.timeDifference;
            self.counter += 1;          
        }, 1);      
    },

    changeFrequency: function(newFrequency){
        var self = this;
        for(var i = 0; i <=19; i+=1){
            //var timeout = setTimeout(function(){ self.setFrequency(i, newFrequency); console.log("frequencyTimeOut: "+self.frequency[i]);}, (this.distance/this.speed)*1*(i+1));
            //this.setFrequency(i, newFrequency);
            var timeout = setTimeout(function(){var j = i; self.frequency[j]=newFrequency; console.log("frequencyTimeOut: "+self.frequency[i]);}, 1000);                
        }       
    }

我试图在超时函数中设置最外层频率数组的值,并在动画间隔内访问此值。但似乎值没有存储在正确的数组中,当我打印出控制台中的值时,它是未定义的。有人可以解释一下如何在我的超时功能中指定正确的数组吗?

我省略了一些代码,因为我觉得它不重要。

0 个答案:

没有答案