1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
| (function () { if ( (Object.hasOwn && Object.hasOwn(Date.prototype, 'leftDown')) || Date.prototype.hasOwnProperty('leftDown') ) { throw new Error('倒计时方法名重复了...想别的招儿吧!'); }
function gtNine(val) { return val > 9 ? `${val}` : `0${val}`; }
function hasOwnProp(obj, prop) { return obj !== null && typeof obj === 'object' && obj.hasOwnProperty(prop); }
Date.prototype.leftDown = function (callback) { if ( hasOwnProp(this, 'paused') && hasOwnProp(this, 'resumed') && hasOwnProp(this, 'canceled') && hasOwnProp(this, 'kept') ) { console.warn( 'leftDown 方法不能在一个实例上重复调用, 可以使用返回的 pause, cancel, resume 方法控制当前倒计时, 或者创建新的 Date 实例调用此方法' ); return; }
var _timer = null; var _referrerTime = Date.now(); var _paused = false; var _canceled = false; var _resumed = false; var _keep = true; var obj = { days: '00', hours: '00', minutes: '00', seconds: '00' }; var evt = new Event('leftDown', { bubbles: true, cancelable: true }); var evtPause = new Event('leftDown:pause', { bubbles: true, cancelable: true }); var evtResume = new Event('leftDown:resume', { bubbles: true, cancelable: true }); var evtCancel = new Event('leftDown:cancel', { bubbles: true, cancelable: true }); evt.leftDown = obj;
var getDiff = function () { _referrerTime = _keep ? _referrerTime + 1000 : Date.now(); return this.getTime() - _referrerTime; }.bind(this);
var calcLeftTimeFn = function () { var leftTime = getDiff(); if (leftTime < 0) { clearInterval(_timer); _timer = null; _canceled = true; evtCancel.leftDown = evt.leftDown = obj; window && window.dispatchEvent(evt); window && window.dispatchEvent(evtCancel); return typeof callback === 'function' && callback(obj); } var d = Math.floor(leftTime / 1000 / 60 / 60 / 24); var h = Math.floor((leftTime / 1000 / 60 / 60) % 24); var m = Math.floor((leftTime / 1000 / 60) % 60); var s = Math.floor((leftTime / 1000) % 60);
obj['days'] = gtNine(d); obj['hours'] = gtNine(h); obj['minutes'] = gtNine(m); obj['seconds'] = gtNine(s); evt.leftDown = obj; window && window.dispatchEvent(evt); typeof callback === 'function' && callback(obj); };
if (getDiff() <= 0) { _canceled = true; evtCancel.leftDown = evt.leftDown = obj; window && window.dispatchEvent(evt); window && window.dispatchEvent(evtCancel); return typeof callback === 'function' && callback(obj); }
_timer = setInterval(calcLeftTimeFn.bind(this), 1000);
Object.defineProperties(this, { paused: { get() { return _paused }, set(newVal) { console.log('paused is the read-only property'); return 'paused is the read-only property'; }, }, resumed: { get() { return _resumed }, set(newVal) { console.log('resumed is the read-only property'); return 'resumed is the read-only property'; }, }, canceled: { get() { return _canceled }, set(newVal) { console.log('canceled is the read-only property'); return 'canceled is the read-only property'; }, }, kept: { get() { return _keep }, set(newVal) { console.log('kept is the read-only property'); return 'kept is the read-only property'; }, }, left: { get() { return obj }, set() { console.log('left is the read-only property'); return 'left is the read-only property'; }, }, }); var cancel = function () { if(_canceled) return; _canceled = true; if (_timer) { clearInterval(_timer); _timer = null; } evtCancel.leftDown = obj; window && window.dispatchEvent(evtCancel); typeof callback === 'function' && callback(obj); }.bind(this); var pause = function () { if(_canceled || _paused) return; _paused = true; _resumed = !_paused; if (_timer) { clearInterval(_timer); _timer = null; } evtPause.leftDown = obj; window && window.dispatchEvent(evtPause); typeof callback === 'function' && callback(obj); }.bind(this); var resume = function (keep = true) { if(_canceled || _resumed) return; if(_timer){ clearInterval(_timer); _timer = null; } _resumed = true; _paused = !_resumed; _keep = keep === false ? false : true; calcLeftTimeFn(); _timer = setInterval(calcLeftTimeFn.bind(this), 1000); evtResume.leftDown = obj; window && window.dispatchEvent(evtResume); typeof callback === 'function' && callback(obj); }.bind(this);
return { cancel: cancel, pause: pause, resume: resume }; }; })();
window.randRGBA = function (a) { let r = Math.floor(Math.random() * 256); let g = Math.floor(Math.random() * 256); let b = Math.floor(Math.random() * 256); a = a >= 1 ? 1 : Math.random() .toString() .match(/\d\.\d{1}/)[0]; return `rgba(${r},${g},${b},${a})`; };
var date = new Date(Date.now() + 1000000);
var { pause, resume, cancel } = date.leftDown(function (obj) { console.log(obj); document.body.style.color = randRGBA(1); });
|