首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >setTimeout()如何访问聚合物元素的成员

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

Stack Overflow用户
提问于 2019-03-26 04:29:34
回答 1查看 0关注 0票数 0

这个问题在这里已有答案:

  • 循环中的JavaScript闭包 - 简单实用的例子 39答案

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

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

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

我遗漏了一些代码,因为我觉得它并不重要。

EN

回答 1

Stack Overflow用户

发布于 2019-03-26 13:31:59

在Polymer 2和3中,您可以使用箭头函数来确保this正确绑定setTimeout()。例如,这是Polymer 3中的一个简单时钟:

<html>
<body>
<script type="module">
  import { PolymerElement, html } from 'https://unpkg.com/@polymer/polymer/polymer-element.js?module'

  class MyClock extends PolymerElement {

    static get properties () { return {clock: String} }

    ready () {
      super.ready()
      this.updateClock()
    }

    updateClock () {
      this.clock = new Date().toLocaleTimeString()
      setTimeout(() => this.updateClock(), 1000)
    }

    static get template () { return html`Current time: [[clock]]` }
  }

  customElements.define('my-clock', MyClock)
</script>

<my-clock></my-clock>
</body>
</html>

这在Chrome和Firefox中没有polyfill,你可以在CodePen中尝试这个。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100005165

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档