Javascript类-回调中的调用类方法

时间:2018-09-27 11:56:15

标签: javascript

我是javascript类的新手,并认为这将简化我的生活!

以下代码基本上是通过api调用来获取状态并每2秒重复一次。

已将代码移到类中,现在将setTimout回调函数识别为一个函数。错误:“ this.getCommandStatus不是函数”

如何解决此问题?

谢谢!

    class CommandInterface {        

    constructor() {

        this.activeCommands = [];
        this.commandStatusUpdateInterval = 2000;
        this.commandStatusUpdateTimer = null;

    }

    async sendCommand(commandData) {

        var postCommandOptions = {
            url: '',
            type: 'POST',
            dataType: 'json',
            data: commandData.command
        };

        postCommandOptions.url = '/api/v1/command/send';

        try {

            var data = await httpService.post(postCommandOptions);

            this.getCommandStatus(data);

        } catch (err) {

            system.log('Chamber _sendCommand: ' + commandData.command.ObjectId + ' ' + commandData.command.CommandName + ' API Failed', err);

        }

    }

    async getCommandStatus(commandStatus) {

        var getCommandStatusOptions = {
            url: '/api/v1/command/status/' + commandStatus.commandCallbackId,
            type: 'GET',
            dataType: 'json'
        };

        try {

            var data = await httpService.get(getCommandStatusOptions);   

                this.commandStatusUpdateTimer = setTimeout(function() {
                    this.getCommandStatus(data);
                  }, this.commandStatusUpdateInterval);


        } catch (err) {

            system.log('command-interface getCommandStatus API Failed [Polling]', err);

        }

    }

}

0 个答案:

没有答案