I'm misunderstanding setInterval() and values that I expect to be around when it goes to execute are not, so it fails.
function setPairAnimations() {
if (pairs.length > 0) {
if (pairCount < pairs.length) {
intervalNames.push(setInterval(function (pairCount) {
if (topMarker == true) {
pairs[pairCount].pMarker1.setZIndex(201);
pairs[pairCount].pMarker2.setZIndex(200);
topMarker = false;
}
else {
pairs[pairCount].pMarker1.setZIndex(200);
pairs[pairCount].pMarker2.setZIndex(201);
topMarker = true;
}
}, 1000));
++pairCount;
setPairAnimations();
}
}
}
function swapMarkers() {
//first, stop all existing running marker animations
clearIntervals();
if (pairs.length > 0) {
setPairAnimations();
}
if (clusters.length > 0) {
setClusterAnimations();
}
}
The 6th line down bombs: "pairs[pairCount].pMarker1.setZIndex(201);" because pairs[pairCount] is undefined. pairs is a global array of literal objects, each object contains two properties or keys within it. Example.
pairs.push({ pMarker1: markerToggles[0].tMarker1, pMarker2: markerToggles[0].tMarker2 });
pairCount is a global count variable
Pairs is being populated correctly as I expect as per debugger. In this case, it's length is 1, so there should only be one animation started between two marker objects (in some cases it could be more marker sets that get animated, depending on how many were found in the pairs array).
I "believe" the problem is that by the time that setInterval function executes, the pairCount value is no longer what I expect (I assume outside of the bounds of the pairs array). In this call:
intervalNames.push(setInterval(function (pairCount) {
pairCount is undefined, and that's why the line I mentioned above fails. But I'm not sure what to do about it. Is it that I can't pass a global variable into the anonymous function call? I'm not a very experienced programmer, so I hope I explained this correctly and can give more code if needed, but I really think it's just a musunderstanding of how setInterval works in loops and I am simply misusing it. thx.