Go Back   OpenLaszlo Developers Forums > OpenLaszlo Showcase > Show us your code!

Show us your code! Want to show your cool OpenLaszlo components, libraries and code hacks apps to the world? Submit your best code here. We'll showcase the best of the best on the OpenLaszlo site and in the OpenLaszlo project blog, and may even add them to a future release for all to use!

Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
Old 03-17-2009, 08:58 AM
kmeixner kmeixner is offline
Senior Community Member
Join Date: Dec 2006
Location: Toronto, Ontario, Canada
Posts: 636
kmeixner is on a distinguished road
Accurate Looping timer

This is a looping timer class I needed to create for my application, it is more accurate than the LzTimer class. This is written in OpenLaszlo 3.3.3/3.4.0.

Note: If you choose to use it please keep the copyright and GNU license comments in the file it resides.


   loopingtimer class by Sarolta Technologies Inc. copyright  2007

   Accurate looping timer class by Kevin Meixner, Lead Web Software   
   Developer, Sarolta Technologies (www.sarolta.com / www.mixandmash.tv )

   This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see http://www.gnu.org/licenses/

  Class: <loopingtimer>
  Extends: N/A (base class)
  Version: OpenLaszlo 3.3.3/3.4.0

  This class is a looping timer that can be used to call a method to peform
  an action repeatedly after a specified number of milliseconds.

  Override the abstract method reactToTimeChange(theTime) to do something
  useful in your application, theTime will be the time elapsed since the
  timer's last firing, it will be close to the 'timer_resolution' set but
  will be off from about 47ms-78ms in FireFox 2 and 47ms-94ms in IE6
  (see note below for explanation).


  This class originally used the LzTimer class but it was firing up to 256ms
  late, so this has been replaced with a setTimeout() method and embedded
  JavaScript which is more accurate, but still fires about 59ms late on

  For this reason the firing times of this class are approximate but will
  probably fire 47ms to 78ms (about 59ms) on average late. As a workaround
  for this problem the timer uses the system time to calculate how much time
  has actually elapsed since the last timer firing and passes the actual time
  elapsed ('theTime') in milliseconds to the abstract 'reactToTimeChange()'

  <class name="loopingtimer">

    <!-- *** ATTRIBUTES *** -->

    <!-- Public Attributes -->

    @param numnber timer_resolution: number of milliseconds between timer
    firings (default: 40ms)

    Note: OpenLaszlo seems to typically have a lower limit of 47-78
    milliseconds between firings, so setting below this may be useless.
    <attribute name="timer_resolution" type="number" value="40" />

    <!-- Private Attributes -->

    <!--- @keywords private -->
    @param number formertime: used internally to calculate the number of
    elapsed milliseconds since playback was started
    <attribute name="formertime" type="number" value="0" />

    <!--- @keywords private -->
    Used internally for tracking virtual current time in milliseconds
    for pause functionality.
    <attribute name="currenttime" type="number" value="0" />

    <!--- @keywords private -->
    <!--- @param string timer_state: 'PAUSED' | 'COUNTING' -->
    <attribute name="timer_state" type="string" value="PAUSED" />

    <!-- *** METHODS *** -->

    <!-- Public Methods -->

    <!--- @keywords abstract -->
    ABSTRACT METHOD: overwrite to do something useful in your program

    @param number theTime: the time in milliseconds elapsed since playback
    was  started
    <method name="reactToTimeChange" args="theTime">
      if ($debug){
        Debug.write('WARNING: reactToTimeChange(): This is an abstract method that should be overridden to do something useful in your application');
        Debug.write('reactToTimeChange(): Time elapsed since last firing in milliseconds: '+theTime);

    <!--- Start Timer (Note: This will reset timer to 0) -->
    <method name="startTimer">
      this.setAttribute('timer_state', 'COUNTING');
      var now = new Date();
      var rawTime = now.getTime();
      this.setAttribute('formertime', rawTime);


    <!--- Pauses timer at current time -->
    <method name="pauseTimer">
      this.setAttribute('timer_state', 'PAUSED');

    <!--- Resumes timer from time it is paused at -->
    <method name="unpauseTimer">
      this.setAttribute('timer_state', 'COUNTING');
      var now = new Date();
      var rawTime = now.getTime();
      this.setAttribute('formertime', rawTime-this.currenttime);

    <!--- Stop Timer - stops timer and resets to 0  -->
    <method name="stopTimer">

    <!--- Resets Timer to 0 -->
    <method name="resetTimer">
      this.setAttribute('formertime', 0);
      this.setAttribute('currenttime', 0);

    <!-- Private Methods -->

    <!--- @keywords private -->
    Called Internally By Timer

    @param number theTime: the actual time in milliseconds that has passed
    since the last timer firing (will usually be 16-100ms more than timer
    firing interval)
    <method name="doForTime">

      // Prevent Timer Incrementing When Paused
      if (this.timer_state == 'PAUSED')

      var now = new Date();

      var rawTime = now.getTime();

      if (this.formertime != 0)
        var currentTime = rawTime - this.formertime;

      this.setAttribute('currenttime', currentTime);

      // Call Abstract Method:



    <!--- @keywords private -->
    Used internally for timer looping.
    <method name="repeat">

      // This function uses an embedded JavaScript function which
      // can be called via the standard JavaScript setTimeout()
      // method which results in more accurate timer firing then the
      // standard OpenLaszlo LzTimer() class. LzTimer() fired up to
      // 256ms late, while setTimeout() usually fires from
      // only 47ms-78ms

      var f = function(){

      setTimeout(f, this.timer_resolution);


Me: Stack Overflow | Assembla | Twitter | LinkedIn | www.kevinmeixner.com
Company: Web | Facebook | Twitter

Last edited by kmeixner; 03-17-2009 at 09:01 AM. Reason: added code tags around code
Reply With Quote


gnu, license, loop, time, timer

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

All times are GMT -8. The time now is 03:46 PM.

Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2015, Jelsoft Enterprises Ltd.