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 // Evaluate if this node is included in the html 62 if($this.closest('html').length === 0) { 63 stop(); // Release the memory 64 dispatchEvent('removed'); 65 return; 66 } 67 // Calculate the time offset 68 secondsLeft--; 69 if(secondsLeft < 0) { 70 secondsLeft = 0; 71 } 72 lasting = { 73 seconds : secondsLeft % 60, 74 minutes : Math.floor(secondsLeft / 60) % 60, 75 hours : Math.floor(secondsLeft / 60 / 60) % 24, 76 days : Math.floor(secondsLeft / 60 / 60 / 24), 77 weeks : Math.floor(secondsLeft / 60 / 60 / 24 / 7), 78 daysLeft: Math.floor(secondsLeft / 60 / 60 / 24) % 7 79 } 80 for(var i=0; i<handlers.length; i++) { 81 var eventName = handlers[i]; 82 if(values[eventName] != lasting[eventName]) { 83 values[eventName] = lasting[eventName]; 84 dispatchEvent(eventName); 85 } 86 } 87 if(secondsLeft == 0) { 88 stop(); 89 dispatchEvent('finished'); 90 } 91 } 92 triggerEvents(); 93 94 function dispatchEvent(eventName) { 95 var event = $.Event(eventName); 96 event.date = new Date(new Date().valueOf() + secondsLeft); 97 event.value = values[eventName] || "0"; 98 event.toDate = toDate; 99 event.lasting = lasting; 100 switch(eventName) { 101 case "seconds": 102 case "minutes": 103 case "hours": 104 event.value = event.value < 10 ? '0'+event.value.toString() : event.value.toString(); 105 break; 106 default: 107 if(event.value) { 108 event.value = event.value.toString(); 109 } 110 break; 111 } 112 callback.call($this, event); 113 } 114 115 function stop() { 116 clearInterval(interval); 117 } 118 119 function start() { 120 $this.data('countdownInterval', setInterval(delegate($this, triggerEvents), 1000)); 121 interval = $this.data('countdownInterval'); 122 } 123 124 if(interval) stop(); 125 start(); 126 }); 127 } 128})(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.