javascript - 在 Javascript 中的同一对象中使用 setTimeout 函数调用对象方法

标签 javascript settimeout

我用这个把头撞在 table 上,我尝试了在 SE 上找到的答案。请帮忙。

我有一个函数可以轮询 api 来获取新数据。我想要的只是该函数每 10000 毫秒重复一次。我认为以下内容会起作用:

    //SURVIVORS_OBJECT
//Store the suvivor object and append the map markers and some additional parameters to it
var survivorMarkers;
function dz_survivorMarkers(){
    //The survivors will be stored as an 'armaMapMarker' object 
    this.survivors = {};
    //Clear a survivor from the list because by id (eg: has not been returned in our query)
    this.clearSurvivor = function(pId){
        this.survivors[pId].marker.setMap(null);
        delete this.survivors[pId];
    };
    //make the api request from the overwatch data api
    this.getSurvivorsRequest = function(){
        console.log('UDBG:::survivorMarkers.getSurvivors ENTERED');
        //the callback function is passed as well
        api.getSurvivors(this.setSurvivors)
    };
    //Set survivors
    //This is the big one - here we will loop through all our existing survivors and check
    //  if they have been returned in the new reponse. If so, we will update their position on
    //  the map and add a poly line from their last position to their new one
    //We will also check if their info window is open, and will open it again for the user
    //The detail window will be checked if it is open too, and it's contents will be refreshed
    this.setSurvivors = function(pResponse) {
        console.log('UDBG:::survivorMarkers.setSurvivors ENTERED');
        console.log(pResponse);
//          try {
            if(pResponse.errors.length > 0) {
                exceptionHandler.e('SRV100',pResponse.errors.message);  
            } else {
                //First go through all our existing survivors, and see if they are in this request too
                var newSurvivors = {};
                for(var id in pResponse.records) {
                    //console.log('UDBG:::survivorMarkers.setSurvivors looping through survivor[' + id + ']');
                    var newSurvivor = new armaMapMarker('SURVIVOR', pResponse.records[id]);

                    //check if this record is in our existing list of survivors
                    if(this.survivors != null && this.survivors[id] != null) {
                        //set our interface options if any
                        newSurvivor.detailWindowIsOpen = this.survivors[id].detailWindowIsOpen;
                        newSurvivor.infoWindowIsOpen = this.survivors[id].infoWindowIsOpen;
                        //And clear the old data
                        this.clearSurvivor(id);
                    }
                    newSurvivors[id] = newSurvivor;
                }
                //Now go through all our old survivors, and see if they were NOT in the response and can be removed
                for(var id in this.survivors) {
                    if(pResponse.records[id] == null) {
                        this.clearSurvivor(id);
                    }
                }
                this.survivors = newSurvivors;
            }
//          } catch (e) {
//              alert(e);   
//          }
        setInterval(function(){survivorMarkers.getSurvivorsRequest()},5000);
    }
}

它会运行一次,但第二次会出现异常:

Uncaught TypeError: Object [object global] has no method 'clearSurvivor'

最佳答案

survivorMarkers 没有在任何地方定义,只是声明。如果您打算调用新实例,则需要执行以下操作。您还需要将 setInterval 更改为 setTimeout,否则,您将在每次 API 调用完成时生成一个新的间隔,而不是在完成时生成单个新请求前一个的。因此我们有以下内容。

setTimeout(function(){
    this.getSurvivorsRequest();
}.bind(this), 5000);

或者也许:

var thiz = this;
setTimeout(function(){
    thiz.getSurvivorsRequest();
}, 5000);

最后,您传递的回调 this.setSurvivors 未绑定(bind)到实例,因此而是在全局上调用,请使用 api.getSurvivors(this.setSurvivors.bind(这个))

关于javascript - 在 Javascript 中的同一对象中使用 setTimeout 函数调用对象方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17649038/

相关文章:

javascript - 使用 setTimeout 让弹出窗口自行淡出

javascript - 如何将字符串转换为html

javascript - 无法在 setTimeout 内设置 innerHTML

javascript - typescript /ReactJS/setTimeout : 'this' implicitly has type 'any' because it does not have a type annotation. ts

javascript - 尝试使用 settimeout 避免分离函数

javascript - 将类添加到 float 框弹出窗口,单击打开弹出窗口

javascript - 使链接转到其默认 href 以外的其他地方

javascript - 如何确保 API 调用在 React 中加载另一个页面之前完成

javascript - Angular-Carousel 模块加载失败

javascript - 在 koa.js 路由中发送带有 header 的 post 请求