I was having some logic problem with the while loop in JavaScript. So basically I am trying to loop a list of coordinate, at each coordinate, I am executing the moveNext() which will plot a marker and loop until the while loop ends. Here is the code:
var k = 1;
while(k < pointArr.length){
var coordx = pointArr[k].x;
var coordy = pointArr[k].y;
window.setTimeout(function(){
moveNext(coordx, coordy, k);
},500);
k++;
}
And the moveNext() which plot a marker onto the map if the parameter k is larger than global counter variable:
var counter = 0;
function moveNext(coordx, coordy, k){
if(k > counter){
console.log(coordx);
console.log(coordy);
map.graphics.clear();
var point = new esri.geometry.Point({ "x": coordx, "y": coordy, "spatialReference": { "wkid": 3414 } });
var symbol = new esri.symbol.PictureMarkerSymbol('img/transportation/currentLoc.GIF', 30, 30);
var PointGraphic = new esri.Graphic(point, symbol);
map.graphics.add(PointGraphic);
var graphic = PointGraphic;
graphic.setSymbol(symbol);
map.graphics.add(graphic);
counter++;
}
}
However, with these codes, it does not execute the setTimeout point by point plotting marker onto the map. Instead, it loops all the way until the end of the while loop and plot the last marker there. Any guides?
Thanks in advance.