0

I am running into an issue where I want to change the value of my slider controller (just adding 1) each time it passes by a marker. This is my current code

if (marker.numKeys > 0) { value = marker.nearestKey(time).index; }

However, the issue I am running into is that midway between markers it changes value.

2 Answers 2

0

You just need to check if the nearest marker is in the past or the future. If it's in the future, just subtract 1 from its index. This avoids the use of a loop.

if (marker.numKeys > 0) {
    nearestMarker = marker.nearestKey(time);
    if (nearestMarker.time > time){
        nearestMarker.index - 1;
    } else {
        nearestMarker.index;
    }
}

You can shorten this using a ternary operator—at the cost of readability—to

if (marker.numKeys > 0) {
    nearestMarker = marker.nearestKey(time);
    nearestMarker.index - ((nearestMarker.time > time)? 1 : 0 )
}
0

marker.nearestKey picks the closest marker - even if it is in the future.

A simple solution would be to iterate over the markers with a for loop instead:

if (marker.numKeys > 0) {
    let i;
    for (i = 0; i < marker.numKeys; i++) { // For every marker we have, add 1 to i
        if (marker.key(i+1).time > time)   // If the marker is past current time,
            break;                         // stop the loop
    }
    i; // Return i value
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.