javascriptpolymerpolymer-1.0firebase-polymer

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


Solution

  • 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(...)