OnTimer keeping correct time?

Is it slow?

object.settimer 1,10 should give me a 10 milisecond timer; one-hundredth of a second.

So if I counted by 10 every time, ontimer...


Sub Object_OnTimer1
milisec = milisec + 10
End Sub


...milisec should reach 1000 every second, on the second.


For a simple stopwatch I used the following code in a text object:

Code: vbscript
  1. Sub Object_OnTimer1
  2. If milisec > 999 Then
  3. milisec = 0
  4. sec = sec + 1
  5. If sec > 59 Then
  6. min = min + 1
  7. sec = 0
  8. End If
  9. If min > 59 Then
  10. hr = hr + 1
  11. min = 0
  12. End If
  13. If hr = 100 Then object.KillTimer 1
  14. Else
  15. milisec = milisec + 10
  16. End If
  17. txt = "0"
  18. If len(milisec) = 1 Then
  19. milisec = txt & txt & milisec
  20. Elseif len(milisec) = 2 then
  21. milisec = txt & milisec
  22. End if
  23. If len(sec) = 1 Then sec = txt & sec
  24. If len(min) = 1 Then min = txt & min
  25. If len(hr) = 1 Then hr = txt & hr
  26. Object.text = hr & ":" & min & ":" & sec & ":" & milisec
  27. End Sub


I let it run side by side with my system clock and my stopwatch is slower than the system time. It takes roughly TWO seconds for milisec to count to 1000. Am I missing something painfully obvious here?
1,855 views 2 replies
Reply #1 Top
Ah, fun I know what you mean. You have to either somehow sync it to your system clock but, even then there will be a slight delay.

I did something like this for practice some time ago and the only work-a-round I figure was two timers. In the first timer you'd put some type of IF STATEMENT to check against the rollover of the system clock.

Code: vbscript
  1. Object.OnTimer 1,1
  2. Sub Timer1
  3. If seconds=0 Then
  4. Object.OnTimer 2,10
  5. End If
  6. End Sub
Reply #2 Top
Thanks, SirS. I went with 2 timers. Since the 1-second timer keeps better time than the millisecond timer, I have the millisecond timer track the milliseconds and the 1-second timer keep track of everything else. Works better now.  



Code: vbscript
  1. Dim milisec, sec, min, hr
  2. 'Called when the script is executed
  3. Sub Object_OnScriptEnter
  4. milisec = 0
  5. sec = 0
  6. min = 0
  7. hr = 0
  8. object.SetTimer 1,1000
  9. object.SetTimer 2,10
  10. End Sub
  11. Sub Object_Ontimer1
  12. milisec = 0
  13. sec = sec + 1
  14. If sec > 59 Then
  15. min = min + 1
  16. sec = 0
  17. End If
  18. If min > 59 Then
  19. hr = hr + 1
  20. min = 0
  21. End If
  22. If hr = 100 Then object.KillTimer 1
  23. End Sub
  24. Sub Object_OnTimer2
  25. If milisec > 999 Then
  26. milisec = 0
  27. Else
  28. milisec = milisec + 10
  29. End If
  30. txt = "0"
  31. If len(milisec) = 1 Then
  32. milisec = txt & txt & milisec
  33. ElseIf len(milisec) = 2 Then
  34. milisec = txt & milisec
  35. End If
  36. If len(sec) = 1 Then sec = txt & sec
  37. If len(min) = 1 Then min = txt & min
  38. If len(hr) = 1 Then hr = txt & hr
  39. Object.text = hr & ":" & min & ":" & sec & ":" & milisec
  40. End Sub