Zvi Karp
Zvi Karp

Reputation: 3904

Calling functions within Polymer element

I'm struggling to call functions inside my polymer element. I know that you need to use this.functionName();, and it works.

but when I was it in a setTimeout like this: runSoon = setTimeout(this.runNow(), 12000); it runs without waiting. If I write it like this: runSoon = setTimeout(function(){this.runNow()}, 12000); it gives me an error message: Uncaught TypeError: this.runNow is not a function.

Also, when I use this.functionName in a Firebase it works, but in "forEach", like in this example, it gives my that error Uncaught TypeError: this.myFunction is not a function:

ref.once('value', function(snapshot) {
  snapshot.forEach(function(child) {
    this.myFunction();
  });
});

Thanks

Upvotes: 1

Views: 950

Answers (1)

Günter Zöchbauer
Günter Zöchbauer

Reputation: 657108

It should be without ()

runSoon = setTimeout(this.runNow, 12000);

this way you pass a referenc to the function this.runNow

runSoon = setTimeout(this.runNow(), 12000);

passes the result of this.runNow() to setTimeout(...)

Upvotes: 5

Related Questions