Skip to content

Instantly share code, notes, and snippets.

@apipkin
Created March 4, 2010 23:12
Show Gist options
  • Save apipkin/322231 to your computer and use it in GitHub Desktop.
Save apipkin/322231 to your computer and use it in GitHub Desktop.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Timer Example</title>
<script src="http://yui.yahooapis.com/3.0.0/build/yui/yui-min.js"></script>
<script>
YUI({
'filter' : 'raw',
'modules': {
'timer' : {
'fullpath' : 'timer.js',
'requires' : ['base','event-custom']
}
}
}).use('timer','node','event',function(Y){
var t = new Y.Timer({length:2005,repeatCount:2,callback:myCallback});
Y.on('click',function(e){ t.start() },'#start-timeout');
Y.on('click',function(e){ t.stop() },'#stop-timeout');
Y.on('click',function(e){ t.pause() },'#pause-timeout');
Y.on('click',function(e){ t.resume() },'#resume-timeout');
t.on('timer:start',function(e){Y.log('MY TIMER STARTED!!!','warn')});
t.on('timer:stop',function(e){Y.log('MY TIMER STOPPED!!!','warn')});
function myCallback() {
Y.log('THIS IS MY CALLBACK','warn');
}
});
</script>
</head>
<body>
<button id="start-timeout">Start Timeout</button>
<button id="stop-timeout">Stop Timeout</button>
<button id="pause-timeout">Pause Timeout</button>
<button id="resume-timeout">Resume Timeout</button>
</body>
</html>
YUI.add('timer',function(Y){
var Timer,
NAME = 'timer',
NS = 'timer',
STATUS = {
RUNNING : 'running',
PAUSED : 'paused',
STOPPED : 'stopped'
},
EVENTS = {
START : 'start',
STOP : 'stop',
PAUSE : 'pause',
RESUME : 'resume',
TIMER : 'timer'
}
;
Timer = function(config) {
Timer.superclass.constructor.apply(this,arguments);
};
Y.extend(Timer, Y.Base,{
initializer : function(config){
timerCallback = Y.bind(this._timer,this);
this.after('statusChange',this._statusChanged,this);
this.publish(EVENTS.START , { defaultFn : this._startEvent });
this.publish(EVENTS.STOP , { defaultFn : this._stopEvent });
this.publish(EVENTS.PAUSE , { defaultFn : this._pauseEvent });
this.publish(EVENTS.RESUME , { defaultFn : this._resumeEvent });
},
start : function() {
Y.log('Timer::start','info');
if(this.get('status') !== STATUS.RUNNING) {
this.fire(EVENTS.START);
}
return this;
},
stop : function() {
Y.log('Timer::stop','info');
if(this.get('status') === STATUS.RUNNING) {
this.fire(EVENTS.STOP);
}
return this;
},
pause : function() {
Y.log('Timer::pause','info');
if(this.get('status') === STATUS.RUNNING) {
this.fire(EVENTS.PAUSE);
}
return this;
},
resume : function() {
Y.log('Timer::resume','info');
if(this.get('status') === STATUS.PAUSED) {
this.fire(EVENTS.RESUME);
}
return this;
},
_makeTimer : function() {
var id = null;
var repeat = this.get('repeatCount');
if(repeat == 0 || repeat > this.get('step')) {
id = Y.later(this.get('length'), this, this._timer);
}
this.set('timer',id);
this.set('start', Date.now());
},
_destroyTimer : function() {
this.get('timer').cancel();
this.set('stop',Date.now());
this.set('step',0);
},
_timer : function() {
Y.log('Timer::_timer','info');
this.fire(EVENTS.TIMER);
var step = this.get('step');
var repeat = this.get('repeatCount');
this.set('step', ++step);
if(repeat > 0 && repeat <= step) {
this.stop();
}else{
this._makeTimer();
}
(this.get('callback'))();
},
_statusChanged : function(e){
switch (this.get('status')) {
case STATUS.RUNNING:
this._makeTimer();
break;
case STATUS.STOPPED: // overflow intentional
case STATUS.PAUSED:
this._destroyTimer();
break;
}
},
_startEvent : function(e) {
this.set('status',STATUS.RUNNING);
},
_stopEvent : function(e) {
this.set('status',STATUS.STOPPED);
},
_pauseEvent : function(e) {
this.set('status',STATUS.PAUSED);
},
_resumeEvent : function(e) {
var remaining = this.get('length') - (this.get('stop') - this.get('start'));
Y.later(remaining,this,function(){this.set('status',STATUS.RUNNING);});
}
},{
NAME : NAME,
NS : NS,
ATTRS : {
callback : { // custom method to call on timer event
value : null
},
length : { // time per interval in ms
value : 3000
},
repeatCount : { // number of times to repeat
setter : function(val) {
if(this.get('state') !== STATUS.STOPPED) {
Y.fail(EXCEPTIONS.NOT_STOPPED_REPEAT_COUNT);
}
return parseInt(val,10);
},
value : 0
},
start : { // time of start
readonly : true
},
state : { // current timer state
value : STATUS.STOPPED,
readonly : true
},
step : { // number of intervals passed
value : 0,
readonly : true
},
stop : { // time of stop/pause
readonly : true
},
timer : { // id of the setTimeout
readonly : true
}
},
STATUS : STATUS,
EVENTS : EVENTS
});
Y.augment(Timer, Y.EventTarget);
Y.Timer = Timer;
},'0.1',{requires:['base','event-custom']});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment