I am in need of a PeriodicalExecuter that I would start and stop
regularly and quite a lot. The doc tells me that I should create a new
one each time, but it doesn't looks like good memory management
practice to me.

So here is a proposition for an upgrade to PeriodicalExecuter  (note
that I am only really interested in the 'start' method, the rest is
just there for completion).

Pascal (aka pvk in the comments)

var PeriodicalExecuter = Class.create({
  initialize: function(callback, frequency) {
    this.callback = callback;
    this.frequency = frequency;
    this.currentlyExecuting = false;
    // Modif PVK        : public flag that could be query to check if a
timer had been paused
    this.active = false;

    this.registerCallback();
  },

  registerCallback: function() {
    this.timer = setInterval(this.onTimerEvent.bind(this),
this.frequency * 1000);
    // Modif PVK        : public flag that could be query to check if a
timer had been paused
        this.active = true;
  },

  execute: function() {
    this.callback(this);
  },

  stop: function() {
    if (!this.timer) return;
    clearInterval(this.timer);
    this.timer = null;
        // Modif PVK
    this.active = false;
  },

// Addition PVK : just make sure that the timer is active, in case we
stop it earlier
  start: function() {
    if (this.timer) return;
    this.registerCallback();
  },

// Addition PVK : force a restart : not sure it this is meaningfull
for public use, but there for
// the following additions
  restart: function() {
    this.stop();
    this.registerCallback();
  },

// Addition PVK
  changeFrequency: function(frequency) {
    this.frequency = frequency;
    this.restart();
  },

// Addition PVK
  changeCallback: function(callback) {
    this.callback = callback;
    this.restart();
   },

  onTimerEvent: function() {
    if (!this.currentlyExecuting) {
      try {
        this.currentlyExecuting = true;
        this.execute();
      } finally {
        this.currentlyExecuting = false;
      }
    }
  }
});


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Prototype: Core" group.
To post to this group, send email to prototype-core@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/prototype-core?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to