I am trying to make a stop watch that reverse counts while the app is stopped. I used "the seconds" function and save "the seconds" into specialFolderPath("documents") whenever counting is stopped. so the saved seconds can be used for resuming count. but the problems is is has no accuracy..
Goal: Auto counting while the counting is stopped or app is shutdown.
Problem : lack of accuracy. when counting is resumed, the number in field "time" is increased.
reproduce this problem :
#1. click "start/resume" then it's reverse counting.
#2. click "stop" then the number stops. e.g. 22 <--8 sec passed from 30
#3. wait few seconds (5~10 sec) then click "start/resume"
#4.Problem will be found.-->the number in the field should be less than the prev number (e.g. the number should be less than 22) but sometimes it's higher than the previous number.
I attached a stack, please help if you have a better approach.
Thanks a lot in advance.
Cheers,
Louis
Code: Select all
---on start/resume button
global t1, t2, tDuration, tRemained, tStop, tTime
on mouseUp
put empty into t1
put empty into t2
put empty into tRemained
put empty into tStop
put 30 into tDuration ## start point
getTimeRemained
startCount
end mouseUp
on getTimeRemained
if url("file:" & specialFolderPath("documents") & "/timeCounter.txt") is not empty then ##if there is a saved seconds
put url("file:" & specialFolderPath("documents") & "/timeCounter.txt") into t1 ##the seconds saved from the last session
put the seconds into t2 ## the current seconds
if tDuration < (t2-t1) then ## time passed for more than 30 sec
answer "Time's up"
else if tDuration = (t2-t1) then ## time passed for 30 sec
answer "Time's up"
else if tDuration > (t2-t1) then
put (tDuration - (t2-t1) ) into tRemained
end if
else ##if it starts fresh
put the seconds into t1
put 30 into tRemained
end if
end getTimeRemained
on startCount
repeat until field "time" = 0
if tStop = true then
exit repeat
end if
put the seconds into t2
put (tRemained-(t2-t1)) into tTime
put tTime into field "time"
wait for 100 millisec with messages
end repeat
end startCount
------on stop button
global tStop
on mouseUp
put true into tStop
put the seconds into url("file:" & specialFolderPath("documents") & "/timeCounter.txt") ## save current seconds
end mouseUp