Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I would like to make a countdown timer, but cannot find a reliable method to make the DateList[] function reverse.

The not so robust code for the Digital Clock with the display of seconds follows:

digit[i_, xoffset_] := Module[{params},
   params = Part[segmentParams, segmentPieces[[i + 1]]];
   params = Map[# + {0, xoffset, 0, 0} &, params];
   Graphics[makeSegment /@ params]];

makeSegment[params_] := 
 Translate[
  Rotate[Polygon[{{0, 0}, {params[[4]], 0}, {params[[4]] + 1/4, 
  1/4}, {params[[4]], 1/2}, {0, 1/2}, {-1/4, 1/4}}], 
   params[[1]] Degree], {params[[2]], params[[3]]}]
segmentParams = {{0, 0, 1.2, 1}, {90,-.6, .6, .6}, {90,1,.6,.6}, {0, 0, 0, 1},
     {90,-.6,-.6,.6}, {90, 1,-.6,.6}, {0, 0,-1.2, 1}};
segmentPieces = {{1, 2, 3, 5, 6, 7}, {3, 6}, {1, 3, 4, 5, 7}, {1, 3, 4, 6, 7},
 {2, 3, 4, 6}, {1, 2, 4, 6, 7}, {1, 2, 4, 5, 6, 7}, {1, 3, 6}, 
 {1, 2, 3, 4, 5, 6, 7}, {1, 2, 3, 4, 6, 7}};
timer = Dynamic@Refresh[Show[{digit[Floor[DateList[][[6]]/10], 12], 
 digit[Mod[Floor@DateList[][[6]], 10], 15]}, ImageSize -> 26], UpdateInterval -> 1]

I say not so robust because after it's running, it has a tendency to stop. Not sure why yet, but none the less I would like to make it run backwards. I can use: RunScheduledTask to begin the timer and set an alarm but would like to show the time counting down.

share|improve this question
3  
migrated to theoretical physics -- ;-) –  Mr.Wizard Jul 22 '12 at 1:25
1  
@Mr.Wizard I wonder if he meant "backwards into the future" ;-) –  Vitaliy Kaurov Jul 22 '12 at 1:27
2  
There is too much code here and stress on digital display. I am also not really sure what the author wants. So I just added a question which may (or may not) be helpful in this case. mathematica.stackexchange.com/questions/8595/… –  Vitaliy Kaurov Jul 22 '12 at 2:25
1  
@Mr.Wizard Comes in handy when you just need another minute ;-) thanks! –  R Hall Jul 22 '12 at 2:50
add comment

1 Answer

up vote 3 down vote accepted

Replace your timer = .. by

tf = IntegerPart[AbsoluteTime[] + 60];

Dynamic@Refresh[
  Show[{digit[#[[-2]], 12], digit[#[[-1]], 15]}, ImageSize -> 26] &@
   IntegerDigits[1000 + tf - IntegerPart@AbsoluteTime[]],   UpdateInterval -> 1]

Edit

To force the countdown to stop after reaching zero, you could do something like:

tf = IntegerPart[AbsoluteTime[] + 10];
expired = False;
Dynamic@Refresh[
  If[(! expired ) && # != {1, 0, 0}, 
     Show[{digit[#[[-2]], 12], digit[#[[-1]], 15]}, ImageSize -> 26],
     (expired = True; digit[0, 12])] &@
              IntegerDigits[100 + tf - IntegerPart@AbsoluteTime[]], 
  UpdateInterval -> 1]
share|improve this answer
    
Great solution. Thank you –  R Hall Jul 22 '12 at 2:53
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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