Created
April 10, 2013 15:25
-
-
Save justinobney/5355612 to your computer and use it in GitHub Desktop.
* This provides a countdown with events that can be listened for * Events: done, tick ( or progress ) * Methods: init, reset
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Countdown = function ($, undefined) { | |
'use strict'; | |
var interval, | |
initialMinutes = 5, | |
initialSeconds = 0, | |
dfd = $.Deferred(); | |
var vars = { | |
isInitialized: false, | |
minutes: initialMinutes, | |
seconds: initialSeconds | |
}; | |
function Init(settings) { | |
if (vars.isInitialized) { | |
throw { | |
name: "Multiple init exception", | |
message: "Initializing multiple times is not allowed." | |
}; | |
} | |
vars.isInitialized = true; | |
vars = $.extend({}, vars, settings); | |
initialMinutes = vars.minutes; | |
initialSeconds = vars.seconds; | |
_Start(); | |
return; | |
} | |
function _Start() { | |
interval = setInterval(function () { | |
if (vars.seconds === 0) { | |
if (vars.minutes === 0) { | |
dfd.resolve(); | |
clearInterval(interval); | |
return; | |
} else { | |
vars.minutes--; | |
vars.seconds = 60; | |
} | |
} | |
vars.seconds--; | |
dfd.notify({ | |
minutes: vars.minutes, | |
seconds: vars.seconds | |
}); | |
}, 1000); | |
} | |
function Reset() { | |
vars.minutes = initialMinutes; | |
vars.seconds = initialSeconds; | |
} | |
return { | |
init: Init, | |
promise: dfd.promise(), | |
done: dfd.promise().done, | |
progress: dfd.promise().progress, | |
reset: Reset | |
}; | |
}; | |
Countdown.create = function () { | |
var newApp = Countdown(jQuery); // Pass in dependancies(); | |
return newApp; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment