1function Timer() { 2 return function(duration) { 3 var _currentDuration = duration, _listeners = { 4 start : [], 5 progress : [], 6 stop : [], 7 }, _intervalId = null; 8 9 return { 10 addListener : addListener, 11 refreshDuration : refreshDuration, 12 start : start, 13 stop : stop, 14 currentDuration : _currentDuration, 15 isStopped : isStopped 16 }; 17 18 function isStopped() { 19 return !_intervalId; 20 } 21 22 function eachListener(event, params) { 23 var events = _listeners[event]; 24 if (!events) { 25 return; 26 } 27 for (var i = 0; i < events.length; i++) { 28 events[i](params); 29 } 30 } 31 32 function refreshDuration(duration) { 33 _currentDuration = duration; 34 } 35 36 function startEvent(tInfo) { 37 eachListener('start', tInfo); 38 } 39 function progressEvent(tInfo) { 40 eachListener('progress', tInfo); 41 } 42 43 function stopEvent(tInfo) { 44 eachListener('stop', tInfo); 45 } 46 47 function _resolveFormat(number) { 48 return number < 10 ? '0' + number : number; 49 } 50 51 function _configureInterval() { 52 var minutes = _resolveFormat(parseInt(_currentDuration / 60, 10)), seconds = _resolveFormat(parseInt( 53 _currentDuration % 60, 10)); 54 55 progressEvent({ 56 duration : _currentDuration, 57 minutes : minutes, 58 seconds : seconds, 59 }); 60 61 if (--_currentDuration < 0) { 62 stop(); 63 } 64 } 65 66 function start() { 67 if (_intervalId) { 68 console.warn('The timer has been already started...'); 69 return; 70 } 71 72 startEvent({ 73 duration : _currentDuration, 74 }); 75 _intervalId = setInterval(_configureInterval, 1000); 76 } 77 78 function stop() { 79 if (_intervalId) { 80 clearInterval(_intervalId); 81 _intervalId = null; 82 stopEvent({ 83 duration : _currentDuration, 84 }); 85 } else { 86 console.warn('The timer is not running...'); 87 } 88 } 89 90 function addListener(event, func) { 91 _listeners[event].push(func); 92 } 93 }.apply(null, arguments); 94}
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.