Hiển thị các bài đăng có nhãn this. Hiển thị tất cả bài đăng
Hiển thị các bài đăng có nhãn this. Hiển thị tất cả bài đăng

Pass correct “this” context to setTimeout callback

Source: https://stackoverflow.com/questions/36209784/variable-inside-settimeout-says-it-is-undefined-but-when-outside-it-is-defined


The reason for this is that the callback function inside setTimeout is in a different lexical environment. This is why in ES6+ functions can be defined using =>. This is so that the code within a function shares the same scope as the function.
To fix this, you can either use ES6+ syntax, where instead of function(a,b,args) {...} you would use (a,b,args) => {...}:
setTimeout( () => {
  this.http.post(...)
});
or with ES5 syntax:
var root = this;

setTimeout(function(){
    root.http.post(...)
}
Hope this helps!

Renewing Facebook Graph API token automatically?

  Mã truy cập dài hạn https://developers.facebook.com/docs/facebook-login/guides/access-tokens/get-long-lived/ https://community.n8n.io/t/re...