Looking for some advice here, as I am still learning Javascript, but have been tasked with making some changes to a widget that was authored by one of my predecessors. The widget tracks your path, logs the positions in a log file, and creates a csv file for export. The watchPosition() method takes positions constantly, which while great for accuracy, creates way too many entries in the log file and the csv file. I am looking for the best way to limit logged entries by either setting some sort of logging interval based on time, or by counting loops. I have been playing around with different scripts, and have found one that works fine in a testing environment, but when I insert it into the widget.js file it doesn't seem to do anything. The script basically takes a time stamp before the loop, and another one each time the loop repeats. I set up an array to capture the 'distance' between each time stamp and to capture only unique values (seconds). I then have it set up to log the position whenever the last added value entered into the array is divisible by 2.
The problem I am having is that the variables I defined before the loop aren't being recognized inside the loop, hence the distArray never gets populated. I don't know if this is a hoisting issue, or maybe has to do with the asynchronous nature of the widget.js code. Here is a snippet of my code, and I have also attached the widget.js file. Not looking for anyone to write me code for me, but I am probably must missing something that may be obvious to you guys! Thanks
targetlayer.queryFeatures(query, lang.hitch(this, function(featureSet) {
var ResultFeatures = featureSet.features;
var currenttime = this.TimeStamp();
var distArray = [];
var startTime = new Date().getSeconds();
for(var i = 0; i < ResultFeatures.length; i++){
var loopTime = new Date().getSeconds();
var distance = loopTime - startTime;
var currentLoc = (currenttime + ',' + PositionSet[0].toPrecision(8) + ',' + PositionSet[1].toPrecision(8) + ',' + targetlayer.name + ',' + ResultFeatures.attributes[resultField]);
var currentLocArray = currentLoc.split(",");
resultsArray.push(currentLocArray);
var logValue = this.Log.value += '' + currentLoc;
if (distArray.indexOf(distance) === -1) {
distArray.push(distance);
if (distance % 2 == 0) {
logValue;
} else {
continue;
}
}