簡體   English   中英

從Javascript的同一對象中使用setTimeout函數調用對象方法

[英]calling an object method with a setTimeout function from within the same object in Javascript

OBeen用這個把我的頭撞在桌子上,我已經嘗試了在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未綁定到實例,因此在全局上調用,請使用api.getSurvivors(this.setSurvivors.bind(this))修復。

您創建的對象實例應該是this.getSurvivorsRequest()而不是survivorMarkers

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM