首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >3秒后不能调用函数角4

3秒后不能调用函数角4
EN

Stack Overflow用户
提问于 2018-06-06 11:52:13
回答 2查看 80关注 0票数 0

代码运行良好,但当我再次尝试调用getUsersChats时,如果有一条新消息要显示徽章,

我不能因为它给我这个错误,

代码语言:javascript
复制
 "ERROR TypeError: Cannot read property 'getUsersChat' of undefined"

getUsersChat函数

代码语言:javascript
复制
 getUsersChat() {

    let clientsKeys: Array < any > ;

    let i = 0;
    // tslint:disable-next-line:prefer-const
    let key: any;

    this.mySegService.getChats().subscribe(res => {

      this.clientsShortList = new Array < any > ();
      clientsKeys = Object.keys(res);

      this.totalNumberClients += clientsKeys.length;

      clientsKeys.forEach(clientKey => {

        if (i < ++i && res[clientKey]['messages'] !== undefined ) {
          this.clientsShortList.push({
            name: res[clientKey]['name'],
          });
        }

        if (res[clientKey]['name'] === this.chatcheck ) {

          this.clientsShortList = new Array < any > ();
          this.clientsShortList.push({
            name: res[clientKey]['name'],
            number: this.compare
          });
        }
        this.clientsShortList.reverse();
        console.log(this.clientsShortList);

      });

    });
    this.newmessageNumber(this.clientsShortList);
  }

这是错误出现在setTimeout(() =>)上的地方.我在这里做错了什么,任何帮助都会很好

代码语言:javascript
复制
newmessageNumber(array: any) {
     // tslint:disable-next-line:no-shadowed-variable
     let element;
     let i = 0;

    for (let index = 0; index < array.length; index++) {
      element = array[index]['name'];
    }

    this.mySegService.getChatsLast().subscribe(res => {
       try {
        localStorage.setItem('user', res[0]['user']);
       } catch (error) {
        // nd
       }
       this.chatcheck = localStorage.getItem('user');
      // console.log(this.chatcheck);
       if (this.chatcheck === element ) {
            this.compare = '1';
      }
    });

    for ( i ; i < 3; i++) {

      (function(i) {
          setTimeout(() => {
            this.getUsersChat();
          }, 1000 * i);
      })(i);
  }


 }

有人能帮我解决这个问题吗。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-06-06 14:14:57

通过这样做来解决

代码语言:javascript
复制
 getUsersChat() {

        let clientsKeys: Array < any > ;

        let i = 0;
        // tslint:disable-next-line:prefer-const
        let key: any;
        // tslint:disable-next-line:no-shadowed-variable
        let element: any;

        this.mySegService.getChats().subscribe(res => {

          this.clientsShortList = new Array < any > ();
          clientsKeys = Object.keys(res);

          this.totalNumberClients += clientsKeys.length;

          clientsKeys.forEach(clientKey => {

            if (i < ++i && res[clientKey]['messages'] !== undefined) {
              this.clientsShortList.push({
                name: res[clientKey]['name'],
              });
            }


            if (res[clientKey]['name'] === this.chatcheck) {
              this.clientsShortList = new Array < any > ();
              this.clientsShortList.push({
                name: res[clientKey]['name'],
                number: '1'
              });
            }

            this.clientsShortList.reverse();
            console.log(this.clientsShortList);
          });

        });
        // this.newmessageNumber(this.clientsShortList);
        for (let index = 0; index < this.clientsShortList.length; index++) {
          element = this.clientsShortList[index]['name'];
        }

        this.mySegService.getChatsLast().subscribe(res => {
          // console.log(res);
          try {
            localStorage.setItem('user', res[0]['user']);
          } catch (error) {
            // nd
          }
          this.chatcheck = localStorage.getItem('user');
          console.log(this.chatcheck);
          if (this.chatcheck === element) {
            this.getUsersChat();
          }

        });



}
票数 0
EN

Stack Overflow用户

发布于 2018-06-06 12:01:40

因为getUsersChat不在setTimeout中的这个范围内。

试着保留这个范围并像这样使用它。

代码语言:javascript
复制
newmessageNumber(array: any) {
     // tslint:disable-next-line:no-shadowed-variable
     let element;
     let i = 0;

    for (let index = 0; index < array.length; index++) {
      element = array[index]['name'];
    }

    this.mySegService.getChatsLast().subscribe(res => {
       try {
        localStorage.setItem('user', res[0]['user']);
       } catch (error) {
        // nd
       }
       this.chatcheck = localStorage.getItem('user');
      // console.log(this.chatcheck);
       if (this.chatcheck === element ) {
            this.compare = '1';
      }
    });
    var self=this;
    for ( i ; i < 3; i++) {

      (function(i) {
          setTimeout(() => {
            self.getUsersChat();
          }, 1000 * i);
      })(i);
  }


 }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50719751

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档