CountDown //JavaScript Repository

Description

Supplies countdown with pause and events.
Created: 2006.06.23

Code (Download)

//+ Carlos R. L. Rodrigues
//@ http://jsfromhell.com/classes/countdown [rev. #1]

CountDown = function(){
    this.date = !(this.finished = this.paused = false);
}
CountDown.prototype.start = function(n, e, t, r){
    var o = this;
    r ? o.onResume && o.onResume(o.n) :
    (o.n = o.date ? new Date(n).getTime() : n, o.e = o.date ? new Date(e).getTime() : e,
    o.t = t, o.finished = false, o.onStart && o.onStart(o.n));
    o.d = e < n ? 1 : -1, o.paused = false, o.i = setInterval(function(){
        o.d * (o.n -= o.d * (o.date ? 1e3 : 1)) <= o.e * o.d &&
        (o.finished = !o.stop()) && !clearInterval(o.i) ||
        o.onUpdate && o.onUpdate(o.n);
    }, (o.t || 1) * 1e3);
}
CountDown.prototype.pause = function(t){
    var o = this;
    clearTimeout(o.x), o.paused ? o.start(o.n, o.e, o.t, 1) :
    (o.paused = !clearInterval(o.i), o.onPause && o.onPause(o.n),
    t && (o.x = setTimeout(function(){
        o.start(o.n, o.e, o.t, 1);
    }, t * 1e3)));
}
CountDown.prototype.stop = function(){
    var o = this;
    clearInterval(o.i), clearTimeout(o.x), o.onStop && o.onStop(o.n), o.n = 0;
}

Example (Example)

<input type="text" size="50" id="lala" />

<script type="text/javascript">
//<![CDATA[

var b = document.getElementById("lala");
var t = new CountDown();
t.date = false;
t.onStart = function(n){
    b.value = n;
    b.disabled = false;
}
t.onUpdate = function(n){
    b.value = n;
}
t.onStop = function(n){
    b.value = "Finish: " + n + "!!!";
    b.disabled = true;
}
t.onPause = function(n){
    b.value = "Pause: " + n;
    b.disabled = true;
}
t.onResume = function(n){
    b.value = n;
    b.disabled = false;
}

document.onclick = function(){
    if(t.finished) t.start(5, 10);
    else t.pause(2);
}

t.start(10, 1);

//]]>
</script>

Help

CountDown

CountDown(void)
Generates an instance of CountDown.

Properties

CountDown.date: Boolean = true
If true Indicates that the counter will be based on a Date object, otherwise an Integer containing the seconds.

Methods

CountDown.start(start: Object, end: Object, [time: Number]): void
Starts the counter.
start
determines the origin of the counter, if the "date" property is true, it should be passed a Date, otherwise the number of seconds
end
determines the end of the counter, if the "date" property is true, it should be passed a Date, otherwise the number of seconds
time
atualization interval in seconds
CountDown.pause([seconds: Number]): void
Pauses the counter.
seconds
Number of seconds that it will stay paused. After that the counter restarts.
CountDown.stop(void): void
Stops the counter.

Events

CountDown.onStart(counter: Integer): void
Called when the counter starts.
counter
Current value in miliseconds. If the "date" property is false the value is a decimal
CountDown.onPause(counter: Integer): void
Called when the counter is paused.
counter
Current value in miliseconds. If the "date" property is false the value is a decimal
CountDown.onResume(counter: Integer): void
Called when the counter is restarted after a pause.
counter
Current value in miliseconds. If the "date" property is false the value is a decimal
CountDown.onStop(counter: Integer): void
Called when the counter is stopped.
counter
Current value in miliseconds. If the "date" property is false the value is a decimal
CountDown.onUpdate(counter: Integer): void
Called on each atualization of the counter.
counter
Current value in miliseconds. If the "date" property is false the value is a decimal

Rank (Votes: 37)

1.27