Web AppBuilder Custom Widget - Use setTimeout() with publishData()

406
2
Jump to solution
01-30-2020 06:52 AM
XuanKuai
New Contributor III

The publishData() function won't work inside setTimeout() in my custom widget - returns error: this.publishData() is not a function. But this.publishData() works fine alone. How to get this.publishData() work inside setTimeout()?

0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Esteemed Contributor

Xuan,

  Your issue is going to be a scope issue. The "this" object inside your setTimeout function is not scoped to the widget so this.publishData is not a function. You need to hitch your setTimeout function using lang.hitch.

setTimeout(lang.hitch(this, function(){
  this.publishData(yourdata);
}), 200);

View solution in original post

2 Replies
RobertScheitlin__GISP
MVP Esteemed Contributor

Xuan,

  Your issue is going to be a scope issue. The "this" object inside your setTimeout function is not scoped to the widget so this.publishData is not a function. You need to hitch your setTimeout function using lang.hitch.

setTimeout(lang.hitch(this, function(){
  this.publishData(yourdata);
}), 200);
XuanKuai
New Contributor III

Sharp answer, thank you!

0 Kudos