Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Timer tick count #4

Merged
merged 3 commits into from Oct 15, 2011
View
@@ -34,39 +34,47 @@
// Options is expected to have optional
// callback and element properties
- _lastTick = 0,
- _lastStart = 0,
- _until = 0,
- pauseFlag = false,
- importantStuff = {
- delta: 0
- },
+ var _lastTick = 0,
+ _lastStart = 0,
+ _until = 0,
+ pauseFlag = false,
+ importantStuff = {
+ delta: 0,
+
+ // how many times callback is called
+ ticks: 0
+ },
stop = function() {
var idx = _timerCallbacks.indexOf( _loop );
_timerCallbacks.splice( idx, 1 );
},
_loop = function() {
- var now = Date.now();
- importantStuff.delta = now - _lastTick;
- _lastTick = now;
-
- // Check to see if the timer is paused
- if ( pauseFlag || ( _until != undefined && _lastTick - _lastStart > _until ) ) {
+ var now = Date.now();
+ importantStuff.delta = now - _lastTick;
+ _lastTick = now;
+
+ // Check to see if the timer is paused, or run over until time but ran
+ // at least once
+ if ( pauseFlag ||
+ ( _until != undefined && _lastTick - _lastStart > _until ) &&
+ importantStuff.ticks !== 0 )
+ {
stop();
- if( options.complete ){
- options.complete();
- }
- // return early so callback is not called again
- return;
- }
+ if( options.complete ){
+ options.complete();
+ }
+ } else {
- // If there is a callback pass the importantStuff to it
- if( options.callback ) {
- options.callback( importantStuff );
- }
+ // If there is a callback pass the importantStuff to it
+ if( options.callback ) {
+ options.callback( importantStuff );
+ }
+
+ importantStuff.ticks++;
+ }
};
View
@@ -38,14 +38,14 @@ asyncTest("timer.start(0) calls once", function() {
if (timesCalled > 1) {
timer.pause();
}
-
- setTimeout(function() {
- start();
- }, 100);
}
});
timer.start(0);
+
+ setTimeout(function() {
+ start();
+ }, 100);
});
asyncTest("timers do not get called twice in one frame", function() {