views:

283

answers:

1

I am having trouble getting a class function to run periodically with mootools. It runs one fine, but then I get a function is undefined error. The related code can be seen here: http://gist.github.com/142298

+1  A: 

You are not correctly calling the periodical function, see the MooTools documentation.

In your example, you run the function once and try to use the periodical function on it's return value (hence your first message is logged directly, not after the 1000ms delay):

var Main = new Class({
  Implements: [Options],

  options: {
    releaseDate: '1 January, 2010'
  },

  initialize: function(options){
    this.setOptions(options);
    this.startClock();
  },

  startClock: function(){
    var current = $time();
    var future = new Date(this.options.releaseDate);
    future = future.getTime();

    this.clock = this.iterateClock(current, future).periodical(1000, this);
  },

  iterateClock: function(current, future){
    var difference = future - current;

    var days = Math.floor((difference / (60 * 60 * 24)) / 1000);
    console.log(days);
  }
});

What you want is to periodically call the iterateClock function with a specified period, binding and arguments (as an array):

this.clock = this.iterateClock.periodical(1000, this, [current, future]);
Ronald
he got the same answer on the mootools mail list. another way to solve this is to use anon function as closure and apply the periodical to it as in his original code.
Dimitar Christoff