2

これで頭を机にぶつけて、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);
    }
}

1 回実行されますが、2 回目は例外が発生します。

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

2 に答える 2

4

どこにもsurvivorMarkers定義されておらず、宣言されているだけです。新しいインスタンスを呼び出す場合は、次の行に沿って何かを行う必要があります。また、前のリクエストの完了時に単一の新しいリクエストを生成するのではなく、各 API 呼び出しの完了時に新しい間隔を生成するように、を変更することもsetIntervalできます。setTimeoutしたがって、次のようになります。

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

多分:

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

最後に、渡されたコールバックthis.setSurvivorsはインスタンスにバインドされていないため、代わりにグローバルで呼び出されます。これを で修正しapi.getSurvivors(this.setSurvivors.bind(this))ます。

于 2013-07-15T07:45:20.640 に答える