general purpose timer

otacke's picture
Forums: 

A while ago, I noticed that the memory game uses a timer that is tied to the library closely. It cannot be used for anything else. I decided to create something like a general purpose timer for H5P. And here it is: https://github.com/otacke/h5p-timer. You will also find it attached to this post as an h5p file. It is not a content type, so don't expect it to do anything on its own ;-)

The timer is not intended for time critical applications, but it can easily be used to keep track of time in games, to set a time limit for tasks, or to create other innovative content libraries that need triggers based on time. What about a mod-player that can handle ProTracker files for example? ;-) Also, it might reduce some redundancies across different h5p libraries under the hood.

The timer...

  • can count up and down
  • can use milliseconds internally but only "displays" tenths of seconds and above
  • can be paused
  • keeps track of three different types of counters: clock time (can be modified even if running, e.g to give a time bonus), playing time (the amount of time that the clock has been ticking), and running time (the total running time ignoring pauses)
  • offers three types of notifications using callback functions for each type of counter: notification at a particular point in time, notification after a certain time period has passed, and recurring notifications in fixed intervals
  • offers some static utility functions: check if something is a timecode, convert timecodes to milliseconds and vice versa, extract single time elements like days or seconds from timecode or milliseconds

It may still contain bugs, but I think that someone else should have a closer look at it. I hope that my comments are good enough to help you. Your feedback is highly appreciated!

H5P file: 
icc's picture

Cool, seems like it would cover most of one's needs in a timer. Perhaps Memory Game should start using this soon!

If there's something that could have made it even greater it would be implementing the H5P.EventDispatcher, thus allowing events like start, stop, reset etc.

Great work!

otacke's picture

I'll gladly figure out how the EventDispatcher works and implement it!

otacke's picture

I had a look at the H5P.EventDispatcher. Seems to pretty straight forward. I'll be back home from a business trip to Denmark (where they also just learned about h5p :-D) in about 3.5 hours. Maybe I'll be awake enough to squeeze it in my code today.

otacke's picture

Done. At least basic stuff (play, pause, stop, reset). I'll have another look at the code in order to see if there might be something more I could do - maybe add the counter times as event data or something like that. But not before I've had some sleep. Was a looooong day :-)

thomasmars's picture

Looking good :)

otacke's picture

Thanks! I'll probably write a brief "how to" document resembling your guides, so it becomes easier for people to use it - and especially to hunt bugs.

otacke's picture

I added the Event Dispatcher as already stated above, and I ironed out some flaws. Feel free to check out v0.2-alpha of the H5P-Timer on github or using the file attached to this post. You will also find some documentation about the H5P-Timer functions that you can use in the github wiki.

If you stumble upon anything that in your opinion should be improved, please drop me a note. Especially: What would be left to do so the library might become part of the official H5P releases?

thomasmars's picture

Looks like you have done a very good job, I haven't been able to go through it completely but it looks very comprehensive. Nice!

To become a part of the official H5P release it would have to be used by an official H5P library.

otacke's picture

I knew why I already prepared https://github.com/otacke/h5p-memory-game/tree/H5P-Timer this weekend as a demonstration ;-) Should I just create a pull request for the memory game linking to/mentioning the repository of H5P-Timer?

 

otacke's picture

Create pull request: done.

icc's picture

Thank you! We'll have a look shortly, stay tuned :-)

Has anyone combined this timer to the flashcards? I haven't been able to find anything but I need one. Or if anyone is up for the task? 

This was a great idea...is the one linked the most updated version?  We'd like to use a timer on almost every activity

otacke's picture

Hi Riff!

The one in the repository (https://github.com/otacke/h5p-timer) is the most recent version, yes.

Best,

Oliver