Page 1 of 1

Timer js

Posted: Sun Oct 20, 2013 3:34 pm
by Epiales
Hey all, not sure where I needed to post this, but I come across this js timer, and was curious how it works, or what it even does; as I don't know much about timers or js :( I think one of our members here has either wrote it or worked with it before, but don't remember which one, as I have a billion tidbits of code snippets running around that I've gathered lmao

Code: Select all

Timer = function( seconds, method )
{
    this.domElement = ( document != undefined ? document.createElement('span') : null );

    this.secondsStart = ( seconds != undefined ? seconds : 0 );
    this.method     = ( method == undefined ? false : true );

    this.init();

    return this;
}

Timer.prototype = {
    
    constructor     : Timer,

    days            : 0,
    hours           : 0,
    minutes         : 0,
    seconds         : 0,

    calcDays        : false,
    calcHours       : false,
    calcMinutes     : true,
    calcSeconds     : true,

    daysPoint       : ' days ',
    hoursPoint      : ' hours ',
    minutesPoint    : ':',
    secondsPoint    : '',

    secondsStart    : 0,
    secondsPast     : 0,

    interval        : null,
    domElement      : null,

    append          : function(elem)
                    {
                        if( document == undefined ) return false;

                        document.body.appendChild(this.domElement);
                    },

    enableDays      : function(bool)
                    {
                        this.calcDays = ( bool == undefined ? true : false ); return this;
                    },
    enableHours     : function(bool)
                    {
                        this.calcHours = ( bool == undefined ? true : false ); return this;
                    },
    enableMinutes   : function(bool)
                    {
                        this.calcMinutes = ( bool == undefined ? true : false ); return this;
                    },
    enableSeconds   : function(bool)
                    {
                        this.calcSeconds = ( bool == undefined ? true : false ); return this;
                    },

    format          : function()
                    {
                        return ( this.days > 0 ? this.days + this.daysPoint : '' ) +
                               ( this.hours > 0 ? this.hours + this.hoursPoint : '' ) +
                               ( this.minutes > 0 ? this.minutes + this.minutesPoint : '' ) +
                               ( this.seconds < 10 ? '0' + this.seconds : this.seconds );
                    },

    calculate       : function()
                    {
                        if( this.method == false )
                        {
                            this.seconds = this.secondsStart - this.secondsPast;
                        }
                        else
                        {
                            this.seconds = this.secondsStart + this.secondsPast;
                        }

                        if( this.seconds > 0 )
                        {
                            if( this.calcDays )
                            {
                                this.days = Math.floor(this.seconds / 86400);
                                this.seconds -= this.days*86400;
                            }
                            
                            if( this.calcHours )
                            {
                                this.hours = Math.floor(this.seconds / 3600);
                                this.seconds -= this.hours*3600;
                            }

                            if( this.calcMinutes )
                            {
                                this.minutes = Math.floor(this.seconds/60);
                            }

                            if( this.calcSeconds )
                            {
                                this.seconds = this.seconds - this.minutes * 60;
                            }
                        }
                        else
                        {
                            this.days = 0;
                            this.hours = 0;
                            this.minutes = 0;
                            this.seconds = 0;
                        }
                    },

    loop            : function()
                    {
                        this.secondsPast++;
                        this.calculate();

                        if( this.method == false && this.seconds == 0 )
                        {
                           window.location.reload();
                           return;
                        }

                        if( this.domElement == undefined ) return;
                        this.domElement.innerHTML = this.format();
                    },

    init            : function()
                    {
                        var _this = this;
                        setInterval( function() { _this.loop() }, 1000 )
                    }
}
 

Re: Timer js

Posted: Wed Oct 30, 2013 1:20 pm
by Chris
That looks like my script. It's a timer object.

create a new time instance:

Code: Select all

var time = new Timer();
And watch the magic happen.

Re: Timer js

Posted: Wed Oct 30, 2013 11:06 pm
by Jackolantern
Chris! You see our new banner? :D

Re: Timer js

Posted: Wed Nov 06, 2013 10:55 am
by Chris
Yeah looks awesome! Who made it?

Re: Timer js

Posted: Wed Nov 06, 2013 12:57 pm
by Jackolantern
Zameleon :)