Polymer timer help (Reaching custom tag in an anonymous function)

802 views
Skip to first unread message

office...@gmail.com

unread,
Aug 2, 2014, 11:23:44 PM8/2/14
to polym...@googlegroups.com

<div>{{ time }} seconds ago</div>


<script>
 
Polymer('timer', {,
 ready
: function(){
 setInterval
(function(){this.time++;}, 1000)
 
}
   
});
</script>

Hi there,

I'm new to this, so if I'm not  following posting guidelines very well, apologies in advance.

I simplified this code a little bit, but you get the idea. The issue with the code is, because setInterval() requires me to create an anonymous function, I can't access the custom element from there.

Is there a way of reaching the custom element here? Or i there a way of making a timing without setInterval()?

Any help would be much appreciated.

-officegunner

Iván Todorovich

unread,
Aug 3, 2014, 11:02:25 AM8/3/14
to polym...@googlegroups.com, office...@gmail.com
This is related to js scoping.

You can either bind this to the anonymous function like this:


<div>{{ time }} seconds ago</div>


<script>
 
Polymer('timer', {,
 ready
: function(){

 setInterval
(function(){this.time++;}.bind(this), 1000)
 
}
    
});
</script>

Or you can set this to a variable:


<div>{{ time }} seconds ago</div>


<script>
 
Polymer('timer', {,
 ready
: function(){
   var _this_ = this;
   setInterval(function(){_this_.time++;}, 1000)
 
}
    
});
</script>

Either one should work

are...@gmail.com

unread,
Aug 21, 2014, 5:50:36 AM8/21/14
to polym...@googlegroups.com, office...@gmail.com, ivan.to...@gmail.com
Thank you so much for this!

alexd...@gmail.com

unread,
May 31, 2015, 3:58:07 PM5/31/15
to polym...@googlegroups.com, ivan.to...@gmail.com, office...@gmail.com
Thank you very much for this. 
Reply all
Reply to author
Forward
0 new messages