Checkbox Control Expression

I am a beginning After Effects user, and I am currently trying to create my own expression for the checkbox control. Actually, the way of doing it was fully described by Dan Ebberts in his tutorial: http://library.creativecow.net/articles/ebberts_dan/expression_cont.php (just look for "checkbox" passage). However, I think that creating my own expression (and comparing it with the expression described by the author) would be more useful for me in order to get my own After Effects experience.

I want my animation to run while the checkbox is on, hold at the current position while the checkbox is off and resume from where it left off when the checkbox comes on again. Here is my expression for time remapping:

The expression looks for the nearest "off" and neighbour "on" key (their indexes are "x" and "x+1") and adds amount of time between these keys to the "timeoff" variable. It seems to work fine firstly, but when it comes to the second interval when the switch is "off", it does not add the previous "timeoff" value to the "timeoff" variable, i.e. "timeoff+=timeoff_old" does not work. I have no ideas why that happens, and it would be interesting for me to fix that error. Any help would be much appreciated. Thanks in advance.

Expressions have no memory of what has been calculated on prveious frames. All variables are wiped out. So.... your expression will have to go back and look at all the previous keyframes to figure out what the current value for time remapping should be.

As I said, it is usually more difficult for me to understand somebody's expressions than to write my own ones. So after several attempts I finally modified my expression. I used a "Slider Control" effect in order to write down the duration of all "off" keyframes during my animation. Then I created a code that calculates all those "Slider" values:

timeoff = 0; // the amount of time when our checkbox switch is off
timeoff_old = 0; // this one has been transferred to a Slider Control
switcher = thisComp.layer("Null 1").effect("Checkbox Control").param("Checkbox");

if (switcher > 0) // our switch is on
{if (switcher.valueAtTime(switcher.nearest_key(time).time)>0) //if our nearest key is "on"
{x = (switcher.nearest_key(time).index)-1}// we need an index of the previous "off" switch
else // our nearest key is "off"
{x = switcher.nearest_key(time).index-2}
if (x > 0) // just fixing an error telling there is no keyframe number 0
{timeoff_old = switcher.key(x+1).time - switcher.key(x).time;
for (i=-1; i<=(x-2); i+=2)// we need to add the "timeoff_old" values during all the previous keyframes to "timeoff"
{timeoff+=effect("timeoff_old")("Slider").valueAtTime((switcher.key(x-i).time)+1/30)}// that's how we calculate all those values
a = time - timeoff}
else // if x = 0
{a=time}}
else
{a=0}

[a]

I am sure that Dan's code is more rational. Thanks a lot for your help and comment, Dan. I will now try to understand how your expression works, as well as the one described in your lesson.