calling an object method with a setTimeout function from within the same object in Javascript -


obeen bangin head against desk 1 , have tried answers find on se. please help.

i have function polls api new data. want function repeat every 10000 ms. thought following work:

    //survivors_object //store suvivor object , append map markers , additional parameters var survivormarkers; function dz_survivormarkers(){     //the survivors stored 'armamapmarker' object      this.survivors = {};     //clear survivor list because id (eg: has not been returned in our query)     this.clearsurvivor = function(pid){         this.survivors[pid].marker.setmap(null);         delete this.survivors[pid];     };     //make api request overwatch data api     this.getsurvivorsrequest = function(){         console.log('udbg:::survivormarkers.getsurvivors entered');         //the callback function passed         api.getsurvivors(this.setsurvivors)     };     //set survivors     //this big 1 - here loop through our existing survivors , check     //  if have been returned in new reponse. if so, update position on     //  map , add poly line last position new 1     //we check if info window open, , open again user     //the detail window checked if open too, , it's contents 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 our existing survivors, , see if in request                 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 record in our existing list of survivors                     if(this.survivors != null && this.survivors[id] != null) {                         //set our interface options if                         newsurvivor.detailwindowisopen = this.survivors[id].detailwindowisopen;                         newsurvivor.infowindowisopen = this.survivors[id].infowindowisopen;                         //and clear old data                         this.clearsurvivor(id);                     }                     newsurvivors[id] = newsurvivor;                 }                 //now go through our old survivors, , see if not in response , can 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);     } } 

it run once, second time exception:

uncaught typeerror: object [object global] has no method 'clearsurvivor' 

nowhere survivormarkers defined, declared. if mean call on new instance, need along following lines. want change setinterval settimeout otherwise, generating new interval upon each api call completion, rather generating single new request upon completion of prior one. hence have following.

settimeout(function(){     this.getsurvivorsrequest(); }.bind(this), 5000); 

or perhaps:

var thiz = this; settimeout(function(){     thiz.getsurvivorsrequest(); }, 5000); 

lastly, passed callback, this.setsurvivors not bound instance , instead called on global, fix api.getsurvivors(this.setsurvivors.bind(this)).


Comments