1/* 2 * jQuery The Final Countdown plugin v1.0.0 beta 3 * http://github.com/hilios/jquery.countdown 4 * 5 * Copyright (c) 2011 Edson Hilios 6 * 7 * Permission is hereby granted, free of charge, to any person obtaining 8 * a copy of this software and associated documentation files (the 9 * "Software"), to deal in the Software without restriction, including 10 * without limitation the rights to use, copy, modify, merge, publish, 11 * distribute, sublicense, and/or sell copies of the Software, and to 12 * permit persons to whom the Software is furnished to do so, subject to 13 * the following conditions: 14 * 15 * The above copyright notice and this permission notice shall be 16 * included in all copies or substantial portions of the Software. 17 * 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 22 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 24 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 */ 26(function($) { 27 28 $.fn.countdown = function(toDate, callback) { 29 var handlers = ['seconds', 'minutes', 'hours', 'days', 'weeks', 'daysLeft']; 30 31 function delegate(scope, method) { 32 return function() { return method.call(scope) } 33 } 34 35 return this.each(function() { 36 // Convert 37 if(!(toDate instanceof Date)) { 38 if(String(toDate).match(/^[0-9]*$/)) { 39 toDate = new Date(toDate); 40 } else if( toDate.match(/([0-9]{1,2})\/([0-9]{1,2})\/([0-9]{2,4})\s([0-9]{1,2})\:([0-9]{2})\:([0-9]{2})/) || 41 toDate.match(/([0-9]{2,4})\/([0-9]{1,2})\/([0-9]{1,2})\s([0-9]{1,2})\:([0-9]{2})\:([0-9]{2})/) 42 ) { 43 toDate = new Date(toDate); 44 } else if(toDate.match(/([0-9]{1,2})\/([0-9]{1,2})\/([0-9]{2,4})/) || 45 toDate.match(/([0-9]{2,4})\/([0-9]{1,2})\/([0-9]{1,2})/) 46 ) { 47 toDate = new Date(toDate) 48 } else { 49 throw new Error("Doesn't seen to be a valid date object or string") 50 } 51 } 52 53 var $this = $(this), 54 values = {}, 55 lasting = {}, 56 interval = $this.data('countdownInterval'), 57 currentDate = new Date(), 58 secondsLeft = Math.floor((toDate.valueOf() - currentDate.valueOf()) / 1000); 59 60 function triggerEvents() { 61 secondsLeft--; 62 if(secondsLeft < 0) { 63 secondsLeft = 0; 64 } 65 lasting = { 66 seconds : secondsLeft % 60, 67 minutes : Math.floor(secondsLeft / 60) % 60, 68 hours : Math.floor(secondsLeft / 60 / 60) % 24, 69 days : Math.floor(secondsLeft / 60 / 60 / 24), 70 weeks : Math.floor(secondsLeft / 60 / 60 / 24 / 7), 71 daysLeft: Math.floor(secondsLeft / 60 / 60 / 24) % 7 72 } 73 for(var i=0; i<handlers.length; i++) { 74 var eventName = handlers[i]; 75 if(values[eventName] != lasting[eventName]) { 76 values[eventName] = lasting[eventName]; 77 dispatchEvent(eventName); 78 } 79 } 80 if(secondsLeft == 0) { 81 stop(); 82 dispatchEvent('finished'); 83 } 84 } 85 triggerEvents(); 86 87 function dispatchEvent(eventName) { 88 var event = $.Event(eventName); 89 event.date = new Date(new Date().valueOf() + secondsLeft); 90 event.value = values[eventName] || "0"; 91 event.toDate = toDate; 92 event.lasting = lasting; 93 switch(eventName) { 94 case "seconds": 95 case "minutes": 96 case "hours": 97 event.value = event.value < 10 ? '0'+event.value.toString() : event.value.toString(); 98 break; 99 default: 100 if(event.value) { 101 event.value = event.value.toString(); 102 } 103 break; 104 } 105 callback.call($this, event); 106 } 107 108 $this.bind('remove', function() { 109 stop(); // If the selector is removed clear the interval for memory sake! 110 dispatchEvent('removed'); 111 }); 112 113 function stop() { 114 clearInterval(interval); 115 } 116 117 function start() { 118 $this.data('countdownInterval', setInterval(delegate($this, triggerEvents), 1000)); 119 interval = $this.data('countdownInterval'); 120 } 121 122 if(interval) stop(); 123 start(); 124 }); 125 } 126 // Wrap the remove method to trigger an event when called 127 var removeEvent = new $.Event('remove'), 128 removeFunction = $.fn.remove; 129 $.fn.remove = function() { 130 $(this).trigger(removeEvent); 131 return removeFunction.apply(this, arguments); 132 } 133})(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.