You Are Here:

Community: Developer Discussion Boards

#1 Old How to create a timer - 2007-11-07, 10:52

Join Date: Nov 2007
Posts: 7
orebla's Avatar
orebla
Offline
Registered User
Hi,
I've a problem when I would to create a control code like timer

Example:

When it's 12:00AM the code print a messagge info: "It is 12:00AM!"

I think it's possible with a "while" cicle.
But whit "while" is not more expansive for the RAM??

Thank for answer!
Reply With Quote

#2 Old Re: How to create a timer - 2007-11-07, 11:47

Join Date: May 2007
Posts: 2,737
Location: 21.46 N 72.11 E
croozeus's Avatar
croozeus
Offline
Forum Nokia Champion
Hi orebla

Replying to your subject of the thread how to create a timer

Code:
timer = e32.Ao_timer()
timer.after( time, function_name )
where time is the time in seconds

If you want to print the time then you can use time function from ctime module.And yes you can make a while loop running to print the time at several intervals.

However i didnt get your this question
Quote:
I think it's possible with a "while" cicle.
But whit "while" is not more expansive for the RAM??
Hope that helps

Best Regards
croozeus
Reply With Quote

#3 Old Re: How to create a timer - 2007-11-07, 13:01

Join Date: Nov 2007
Posts: 7
orebla's Avatar
orebla
Offline
Registered User
Sorry for my bad english!

The function AO_timer() checks whether the time, for example, is 12:00 AM.
But the function controls the time only once, when the script is loaded, right?

My question is: But using the while loop to check the time not create a loop infinitive?


I hope that I explained


Sorry for my bad bad english :-( :-(
Reply With Quote

#4 Old Re: How to create a timer - 2007-11-07, 13:14

Join Date: May 2007
Posts: 2,737
Location: 21.46 N 72.11 E
croozeus's Avatar
croozeus
Offline
Forum Nokia Champion
Quote:
Originally Posted by orebla View Post
Sorry for my bad english!

The function AO_timer() checks whether the time, for example, is 12:00 AM.
But the function controls the time only once, when the script is loaded, right?

My question is: But using the while loop to check the time not create a loop infinitive?


I hope that I explained
Hello orebla

Hope I got you correct this time you can use an infinit while loop to print the time at certains intervals like below where time specifies time in seconds.The function_name specifies function which is to be repeated after time seconds and should be define in the begininng.

Code:
timer = e32.Ao_timer()
# loop forever
while True:
    timer.after( time, function_name )
Hope that helps

Best Regards
croozeus
Last edited by croozeus : 2007-11-07 at 13:15. Reason: mistype
Reply With Quote

#5 Old Re: How to create a timer - 2007-11-08, 01:23

Join Date: Oct 2007
Posts: 67
nlsp
Offline
Regular Contributor
This is incorrect:
Code:
timer = e32.Ao_timer()
# loop forever
while True:
    timer.after( time, function_name )
The above will repeatedly call timer.after, as timer.after(time, function_name) will return immediately and call function_name after the number of seconds specified. This will cause a RuntimeError: Timer pending - cancel first.

The correct way (you can search this forum for more examples) is to restart the timer inside the called function.

Code:
import e32
timer = e32.Ao_timer()
delay = 4.0
def do_something():
  # your code to be repeated every interval goes here
  timer.after(delay, do_something)

# still have to start the timer once
timer.after(delay, do_something)
Note that before exiting python, you should call timer.cancel().
One way to do that is using atexit.
Code:
import atexit
atexit.register(timer.cancel)
Last edited by nlsp : 2007-11-08 at 01:29.
Reply With Quote

#6 Old Re: How to create a timer - 2007-11-08, 04:48

Join Date: May 2007
Posts: 2,737
Location: 21.46 N 72.11 E
croozeus's Avatar
croozeus
Offline
Forum Nokia Champion
Hello nlsp

True,we do get a runtime error in my code however its not wrong.The runtime error occurs because the timer is still pending and we need to cancel it.As My code before I now define the body of the function_name as in the below code.

Code:
import e32
def function_name():
    print "Hello"
    timer.after( 4, function_name )

timer = e32.Ao_timer()
# loop forever
while True:
    timer.after( 4, function_name )
The code works and prints hello at every 4 seconds.You are true that the runtime error occurs in this case and so to avoid this we can use this code as well and it would be better as we donot get the runtime error in it!

Code:
import e32
timer = e32.Ao_timer()
delay = 4.0
def do_something():
  # your code to be repeated every interval goes here
  timer.after(delay, do_something)

# still have to start the timer once
timer.after(delay, do_something)
However I donot use the timer = e32.Ao_timer() personally in my applications.I just use the e32.ao_sleep(t) in a while loop when I am required to repeat the function as below

Code:
t=4
While True:
    e32.ao_sleep(t)
    function_name()
Best Regards
croozeus
Reply With Quote

#7 Old Re: How to create a timer - 2007-11-11, 14:39

Join Date: Nov 2007
Posts: 7
orebla's Avatar
orebla
Offline
Registered User
Thanks for answer!!

I understand how use the function Ao_timer.
But how can print a message at twelve o'clock??


Sorry for my bad bad english :-( :-(
Reply With Quote

#8 Old Re: How to create a timer - 2007-11-11, 16:59

Join Date: Oct 2007
Posts: 67
nlsp
Offline
Regular Contributor
Quote:
Originally Posted by orebla View Post
Thanks for answer!!

I understand how use the function Ao_timer.
But how can print a message at twelve o'clock??
I'm not sure there exists a ready to use interface to do something like timer.at(<sometime>, func). However you can get the current time, calculate how many seconds in the future you want to be called back, and use timer.after() to do your thing at the calculated time.
Reply With Quote

#9 Old Re: How to create a timer - 2007-11-13, 16:55

Join Date: Nov 2007
Posts: 7
orebla's Avatar
orebla
Offline
Registered User
Ok I get current time with time.localtime()

but is very difficult for me calculate the difference between two date.

What can I do?


Sorry for my bad bad english :-( :-(
Reply With Quote

#10 Old Re: How to create a timer - 2007-11-13, 22:37

Join Date: Oct 2007
Posts: 67
nlsp
Offline
Regular Contributor
Code:
import e32
import time

def waittime():
  t = list(time.localtime())
  n = time.mktime(t)
  t[3] = t[4] = t[5] = 0
  t[2] += 1
  return time.mktime(t) - n

timer=e32.Ao_timer()
w = waittime()
while w > 60:
  h = w / 3600
  m = w % 3600 / 60
  s = w % 60
  print "waiting %02d:%02d:%02d ..." % (h, m, s)
  timer.after(s or 60)
  w = waittime()

timer.after(w)
print "It's time!!"
Reply With Quote
Reply « Previous Thread | Next Thread »
Display Modes
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules

You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump
Similar Threads
Thread Thread Starter Forum Replies Last Post
When can you set the screensaver's Refresh Timer Value? azhrei Symbian Tools & SDKs 6 2008-08-23 11:12
Crash with multiple ao_timer noreli Python 2 2007-11-01 07:18
inactivity timer, backlight, system dialogue windows. davidmaxwaterman Series 40 & S60 Platform Feedback 0 2007-05-24 04:11
Can anybody help me create a "periodic" timer? supertoync General Symbian C++ 1 2005-10-27 09:41
How to create countdown timer display in the nokia 7610? Hector_D Mobile Java General 0 2005-01-19 03:02

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia 
RDF Facets: qdcZidentifierQSxhttpE3aE2fE2fdiscussionE2eforumE2enokiaE2ecomhttpE3aE2fE2fdiscussionE2eforumE2enokiaE2ecomE2fforumE2fshowthreadE2ephpE3ftE3d81449X qdcZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qdcZtypeQUqfntypeZCommunityContentQ qdcZtypeQUqfntypeZE44iscussionQ qdcZtypeQUqfntypeZE44iscussionContentQ qdcZtypeQUqfntypeZE52esourceQ qdcZtypeQUqfntypeZWebpageQ qdcZtypeQUqmarsZManagedE52esourceQ qdcZtypeQUqwebZInformationE52esourceQ qdcZtypeQUqwebZPageQ qdcZtypeQUqwebZE52esourceQ qdcZtypeQUqrdfsZE52esourceQ qfnZtopicQUqfnTopicZpythonQ qfnZtypeQUqfntypeZCommunityContentQ qfnZtypeQUqfntypeZE44iscussionQ qfnZtypeQUqfntypeZE44iscussionContentQ qfnZtypeQUqfntypeZE52esourceQ qfnZtypeQUqfntypeZWebpageQ qfnZuserE5ftagQSxpythonX qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX qrdfZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qrdfZtypeQUqfntypeZCommunityContentQ qrdfZtypeQUqfntypeZE44iscussionQ qrdfZtypeQUqfntypeZE44iscussionContentQ qrdfZtypeQUqfntypeZE52esourceQ qrdfZtypeQUqfntypeZWebpageQ qrdfZtypeQUqmarsZManagedE52esourceQ qrdfZtypeQUqwebZInformationE52esourceQ qrdfZtypeQUqwebZPageQ qrdfZtypeQUqwebZE52esourceQ qrdfZtypeQUqrdfsZE52esourceQ