H
H
Hanry6542020-07-08 21:56:32
Node.js
Hanry654, 2020-07-08 21:56:32

If I run a function via setTimeout, then it does not work, what should I do?

class dg_commands {
  constructor(token,peer_id) {
    this.vk = new VK({
      token: token, // Token group
    });
    this.peer_id = peer_id;
    this.group_id = -xxxxxxxx
  };

  async send(text) {
    var res = await this.vk.api.messages.send({
      peer_id: this.peer_id,
      message: text
    });
    return res
  };

  async full(){
    setTimeout(this.send, 1000,'text'); // not working
    this.send('text'); // work
  };
};

If you call the function via setTimeout, the response is: "TypeError: Cannot read property 'api' of undefined
"

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dima Polos, 2020-07-08
@Hanry654

You're losing context. See arrow functions or bind

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question