<p>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.</p>

<pre class="lang-js prettyprint-override"><code>if (marker.numKeys &gt; 0) {
    nearestMarker = marker.nearestKey(time);
    if (nearestMarker.time &gt; time){
        nearestMarker.index - 1;
    } else {
        nearestMarker.index;
    }
}
</code></pre>

<p>You can shorten this using a ternary operator—at the cost of readability—to</p>



<pre><code>if (marker.numKeys &gt; 0) {
    nearestMarker = marker.nearestKey(time);
    nearestMarker.index - ((nearestMarker.time &gt; time)? 1 : 0 )
}
</code></pre>

CC BY-SA 4.0
2020-01-27T12:15:35.797Z
2020-01-30T03:55:37.097Z
2020-01-30T03:55:37.097Z
stib
<p><code>marker.nearestKey</code> picks the closest marker - even if it is in the future.  </p>

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

<pre class="lang-js prettyprint-override"><code>if (marker.numKeys &gt; 0) {
    let i;
    for (i = 0; i &lt; marker.numKeys; i++) { // For every marker we have, add 1 to i
        if (marker.key(i+1).time &gt; time)   // If the marker is past current time,
            break;                         // stop the loop
    }
    i; // Return i value
}
</code></pre>

CC BY-SA 4.0
2020-01-27T12:00:03.227Z
2020-01-27T12:00:03.227Z
rebane2001
<p>If anyone is interested, here is <a href="https://community.adobe.com/t5/after-effects-discussions/increasing-the-value-of-a-property-using-markers-with-linear-interpolation/m-p/14210780#M239110" rel="nofollow noreferrer">a solution from the Adobe forums</a>:</p>
<pre><code>m = marker;
val = 0;
if(m.numKeys &gt; 0){
  n = m.nearestKey(time).index;
  if (time &gt;= m.key(n).time) n = Math.min(m.numKeys,n+1);
  t = m.key(n).time - time;
  val = linear(t,0,framesToTime(5),n,n-1);
}
val
</code></pre>

CC BY-SA 4.0
2023-11-05T19:23:47.687Z
2023-11-06T02:06:34.783Z
2023-11-06T02:06:34.783Z
Alexander
<p>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</p>

<p><code>if (marker.numKeys &gt; 0) {
    value = marker.nearestKey(time).index;
}</code></p>

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

<after-effects><after-effects-expressions><after-effects-scripting>
How to change value in After Effects when passing marker
Brice
# How to change value in After Effects when passing marker

(Asked by: username_0 on 2020-01-26)

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.

**Tags:** after-effects, after-effects-expressions, after-effects-scripting


---

## 3 answers

### Answer by username_1 on 2020-01-27. Score: 0 | Accepted answer

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 )
}


---

### Answer by username_2 on 2020-01-27. Score: 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
}


---

### Answer by username_3 on 2023-11-05. Score: 0

If anyone is interested, here is a solution from the Adobe forums:
m = marker;
val = 0;
if(m.numKeys > 0){
  n = m.nearestKey(time).index;
  if (time >= m.key(n).time) n = Math.min(m.numKeys,n+1);
  t = m.key(n).time - time;
  val = linear(t,0,framesToTime(5),n,n-1);
}
val


---

