1/*! 2 * Nu Timer 3 * Copyright (C) 2013 Adam Tavares 4 * http://adamtavares.com 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining 7 * a copy of this software and associated documentation files (the 8 * "Software"), to deal in the Software without restriction, including 9 * without limitation the rights to use, copy, modify, merge, publish, 10 * distribute, sublicense, and/or sell copies of the Software, and to 11 * permit persons to whom the Software is furnished to do so, subject to 12 * the following conditions: 13 * 14 * The above copyright notice and this permission notice shall be 15 * included in all copies or substantial portions of the Software. 16 * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 */ 25;(function ( $ ) { 26 27 //------------------------------------------------------------ 28 // The plugin constructor 29 // 30 // This pattern works well if you need multiple instances 31 // that maintain their own state, but do not require public 32 // methods. 33 // 34 //------------------------------------------------------------ 35 var nu_timer = function( element, options ) { 36 this.element = element; 37 this.options = $.extend( {}, $.fn.nu_timer.defaults, options); 38 this._defaults = $.fn.nu_timer.defaults; 39 this._name = 'nu_timer'; 40 this._nu_timediff = new nu_timediff(); 41 42 this.init = function(){ 43 return true; 44 }; 45 46 this.start = function() { 47 var self = this; 48 49 //------------------------------------------------------ 50 // Make sure you scope these variables for setInterval 51 //------------------------------------------------------ 52 //------------ 53 // configure 54 //------------ 55 var timediff = self._nu_timediff; 56 var end = self.options['end']; 57 timediff.config( end ); 58 59 //---------------------------------- 60 // Dom elements to write values to 61 //---------------------------------- 62 var weeks = self.options['weeks']; 63 var days = self.options['days']; 64 var hours = self.options['hours']; 65 var minutes = self.options['minutes']; 66 var seconds = self.options['seconds']; 67 68 self.go( timediff, weeks, days, hours, minutes, seconds ); 69 70 //------------------- 71 // Run every second 72 //------------------- 73 if ( self.options['run_once'] == false ) { 74 self.options['running'] = setInterval( 75 function() { 76 //-------------------------- 77 // Add one and do the diff 78 //-------------------------- 79 timediff.plus_one(); 80 self.go( timediff, weeks, days, hours, minutes, seconds ); 81 }, 82 1000 83 ); 84 } 85 }; 86 87 this.go = function( timediff, weeks, days, hours, minutes, seconds ) { 88 var self = this; 89 90 //-------------------------------- 91 // Calculate the time difference 92 //-------------------------------- 93 timediff.diff(); 94 95 //------------------------------ 96 // Write the values to the DOM 97 //------------------------------ 98 if ( weeks ) { 99 weeks.text( timediff.data.readable.weeks ); 100 } 101 if ( days ) { 102 days.text( timediff.data.readable.days ); 103 } 104 if ( hours ) { 105 hours.text( timediff.data.readable.hours ); 106 } 107 if ( minutes ) { 108 switch( timediff.data.relative ) { 109 case 'minutes': 110 minutes.text( timediff.data.minutes ); 111 break; 112 default: 113 minutes.text( timediff.data.readable.minutes ); 114 break; 115 } 116 } 117 if ( seconds ) { 118 switch( timediff.data.relative ) { 119 case 'seconds': 120 seconds.text( timediff.data.seconds ); 121 break; 122 default: 123 seconds.text( timediff.data.readable.seconds ); 124 break; 125 } 126 } 127 128 //--------------------- 129 // Check zero & alert 130 //--------------------- 131 self.checkZero( timediff ); 132 } 133 134 this.checkZero = function( timediff ) { 135 var self = this; 136 if ( timediff.data.seconds == 0 ) { 137 //----------------- 138 // Send the alert 139 //----------------- 140 $(self).trigger( 'nu_timer-zero' ); 141 142 //------------------ 143 // Stop if need be 144 //------------------ 145 if ( self.options['stop_at_zero'] == true ) { 146 self.stop(); 147 } 148 } 149 }; 150 151 this.stop = function() { 152 var self = this; 153 clearInterval( self.options['running'] ); 154 self.options['running'] = null; 155 }; 156
157 this.reset = function() { 158 var self = this; 159 self.stop(); 160 self.start(); 161 } 162 163 }; 164 165 //--------------------------------- 166 // Make class available to jQuery 167 //--------------------------------- 168 $.fn.nu_timer = function ( options ) { 169 var id = $(this).selector; 170 return this.each( function() { 171 $.data( this, id, new nu_timer( this, options ) ); 172 }); 173 } 174 175 $.fn.nu_timer.defaults = { 176 'stop_at_zero': true, 177 'run_once': false, 178 }; 179 180})( jQuery );
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.