4

I can't access the 'members' of my 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);                
        }       
    }

I'm trying to set the value of my outermost frequency array inside of a timeout function, and access this value inside of the animation interval. but it seems like the values aren't stored in the right array, when i print out the value in the console it is undefined. Could someone explain me how to specify the right array inside of my timeout function?

I left out some of the code, because i thought it isn't important.

Bob Gilmore
  • 12,608
  • 13
  • 46
  • 53
Nigel N.
  • 307
  • 1
  • 14
  • Regarding the duplicate, in your case you're passing the function to `setTimeout` rather than assigning it (to the `funcs` array in the example), but the principle is the same. – James Thorpe Oct 08 '15 at 14:12
  • Thanks for your quick reply. I think in this example it's the other way around. I want the inner function to write the value in the variable of the outer function. However the function seems to use it's own variable, even though i used self, which i defined as self=this. – Nigel N. Oct 08 '15 at 14:31
  • The issue is more around the use of `i` in the inner function. By the time the timeouts run, the `for` loop will complete, and _all_ the inner functions will have `i` set to 20. – James Thorpe Oct 08 '15 at 14:32
  • Ok, i tried to store the value of i in a local variable of the function, but i still have the same problem. I edited the code, so you can see what i did. I think the problem is that the self.frequency[] array is a local one of the function and not the outer one i'd like to set. – Nigel N. Oct 08 '15 at 14:43
  • 2
    You still have the exact same issue - by the time the inner function runs, `i` is 20, which you now assign to `j` (as 20), and use in the same way. Take a look at the answers on the linked question - they show how to work around this in a few different ways. – James Thorpe Oct 08 '15 at 14:44
  • Ok, i'll look into this. thanks for your help – Nigel N. Oct 08 '15 at 14:46
  • more easy way: pass i as 3th param in setTImeout: setTimeout(function(i){console.log(i)}, 1000,i); – max_well Oct 08 '15 at 14:47
  • Yes, but it's created _within the inner function_ and is assigned the value of `i` that you already had, which is 20. Even if you do `var j = i;` _outside_ of the `setTimeout` and within the `for` loop, you'll have the same problem due to how javascript variables are scoped. Take a look at the other question. – James Thorpe Oct 08 '15 at 14:48
  • Thanks a lot to the two of you :) it's working now. Now that i see it, the problem was actually quite clear. – Nigel N. Oct 08 '15 at 14:55

0 Answers0