-1

I have problem when i'm trying to access by this keyword. I need to access run function inside updateTime

export class PlayerService {

 createPlayer(): void {
            return new window['YT'].Player(this.youtube.playerId, {
                height: this.youtube.playerHeight,
                width: this.youtube.playerWidth,

                events: {
                    'onStateChange': (event) => {

                        function updateTime() {

                   //////I need to access | run function | from here 

                        };

                        this.timeupdater = setInterval(updateTime, 100);


                    }
                }
            });
        }

           run (){

    //some codes 



                }
}

I'm working on ionic 2

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
programmer
  • 423
  • 1
  • 4
  • 13
  • Can you update your question to show all the code and the failure? – Matt Jul 09 '17 at 16:28
  • Make sure to use an arrow function for `updateTime` as well – Bergi Jul 09 '17 at 16:28
  • @Bergi yes that was the problem , I have no idea about this before ( the problem is I have to use arrow in both ) , but i didnt understand how this is could be duplicated question ? there is nothing related from your marking question :) – programmer Jul 09 '17 at 16:45
  • @programmer Of course it is. It explains (all the ways) how to access the `this` value inside a function like `updateTime` that you pass as a callback to something (`setTimeout` in your example), and why it doesn't work with `function`. – Bergi Jul 09 '17 at 18:49

1 Answers1

1

You need to go arrow function all the way

function updateTime() {

should be

var updateTime = () => {

or

setInterval(updateTime, 100);

should be

setInterval(() => updateTime(), 100);
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567