Search code examples
javascriptmootools

Mootools Counter countdown not work


I got a mootools countdown here, but it look like not work
I got an error message Uncaught ReferenceError: PeriodicalExecuter is not defined in CountDown.js line 36 (this.timer = new PeriodicalExecuter(this.update.bind(this), (this.options.frequency || 1000) / 1000);)

The class PeriodicalExecuter seem not included in mootools. Hope someone have the code of PeriodicalExecuter or know where I could find it.

The class PeriodicalExecuter should at least include function stop() and registerCallback()

Here is the code of CountDown.js for your reference

/*
---
script: CountDown.js
license: MIT-style license.
description: CountDown - a mootools countdown implementation.
copyright: Copyright (c) 2008 Thierry Bela
authors: [Thierry Bela]

requires: 
  core:1.2.3: 
  - Events
  - Options
provides: [CountDown]
...
*/

var CountDown = new Class({

    /* 

        options: {

            onChange: $empty,
            onComplete: $empty,
            date: null,
            frequency: 1000 //define the update frequency (in ms), default to 1000
        },

        */
    Implements: [Options, Events],
    initialize: function (options) {

        this.setOptions(options);
        if(!this.options.date instanceof Date) this.options.date = new Date(this.options.date);

        this.timer = new PeriodicalExecuter(this.update.bind(this), (this.options.frequency || 1000) / 1000);
    },
    stop: function () {

        this.timer.stop();          
        return this
    },
    start: function () {

        this.timer.registerCallback();          
        return this
    },
    update: function () {

        var millis = Math.max(0, this.options.date.getTime() - new Date().getTime()),
        time = Math.floor(millis / 1000),
        stop = time == 0,
        countdown = {

            days: Math.floor(time / (60 * 60 * 24)), 
            time: time,
            millis: millis
        };

        time %= (60 * 60 * 24);

        countdown.hours = Math.floor(time / (60 * 60));
        time %= (60 * 60);
        countdown.minutes = Math.floor(time / 60);
        countdown.second = time % 60;

        this.fireEvent('onChange', countdown);

        if(stop) {

            this.timer.stop();
            this.fireEvent('onComplete');
        }
    }
});

EDIT
I am using Mootools version 1.4.5 with compatibility


Solution

  • You could change the class code to use standard methods.

    It looks like .registerCallback is just a function that starts the timer, ie. setInterval. Clearly .stop stops the timer, ie. clearInterval.
    The only thing the PeriodicalExecuter class appears to do is to pass the arguments you gave it on instantiation to the setInterval call every time.

    That's about enough information to implement it yourself :)