I tried your expression and found 2 flaws to it. It said that K1 is a function and it needed a number on line 22, and got out of reach for either markers or layers. So i modified it a bit. Tell me if it works, or what does'nt work, in case something is wrong. This is what i gotn= 0;
if (marker.numKeys > 0){
n = marker.nearestKey(time).index;
if (marker.key(n).time > time){
n--;
}
};