PageSourceSearch

https://www.24hseries.com/libjs/jqueryplugins/jquery.timer.js?1697030868

js 24hseries.com collected 2026-10-02 11:34:10 UTC 5,640 bytes, 277 lines download raw bytes

1/**
2 * jquery.timer.js
3 *
4 * Copyright (c) 2011 Jason Chavannes <[email protected]>
5 *
6 * http://jchavannes.com/jquery-timer
7 *
8 * Permission is hereby granted, free of charge, to any person
9 * obtaining a copy of this software and associated documentation
10 * files (the "Software"), to deal in the Software without
11 * restriction, including without limitation the rights to use, copy,
12 * modify, merge, publish, distribute, sublicense, and/or sell copies
13 * of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be
17 * included in all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
23 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
24 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
25 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * SOFTWARE.
27 */
28
29;(function($) {
30
31    $.timer = Timer;
32
33    /**
34     * First parameter can either be a function or an object of parameters.
35     *
36     * @param {function | {
37     *   action: function,
38     *   time: int=,
39     *   autostart: boolean=
40     * }} action
41     * @param {int=} time
42     * @param {boolean=} autostart
43     * @returns {Timer}
44     */
45    function Timer(action, time, autostart) {
46
47        if (this.constructor != Timer || this.init) {
48            return new Timer(action, time, autostart);
49        }
50
51        this.set(action, time, autostart);
52
53        return this;
54
55    }
56
57    /**
58     * @see Timer
59     *
60     * @param {function | {
61     *   action: function,
62     *   time: int=,
63     *   autostart: boolean=
64     * }} action
65     * @param {int=} time
66     * @param {boolean=} autostart
67     * @returns {Timer}
68     */
69    Timer.prototype.set = function(action, time, autostart) {
70
71        this.init = true;
72
73        if (typeof action == "object") {
74
75            if (action.time) {
76                time = action.time;
77            }
78
79            if (action.autostart) {
80                autostart = action.autostart;
81            }
82
83            action = action.action;
84
85        }
86
87        if (typeof action == "function") {
88            this.action = action;
89        }
90
91        if (!isNaN(time)) {
92            this.intervalTime = time;
93        }
94
95        if (autostart && this.isReadyToStart()) {
96            this.isActive = true;
97            this.setTimer();
98        }
99
100        return this;
101
102    };
103
104    Timer.prototype.isReadyToStart = function() {
105
106        var notActive = !this.active;
107        var hasAction = typeof this.action == "function";
108        var hasTime   = !isNaN(this.intervalTime);
109
110        return notActive && hasAction && hasTime;
111
112    };
113
114    /**
115     * @param {int=} time
116     * @returns {Timer}
117     */
118    Timer.prototype.once = function(time) {
119
120        var timer = this;
121
122        if (isNaN(time)) {
123            timer.action();
124            return this;
125        }
126
127        setTimeout(fnTimeout, time);
128        return this;
129
130        function fnTimeout() {
131            timer.action();
132        }
133
134    };
135
136    /**
137     * @param {boolean=} reset
138     * @returns {Timer}
139     */
140    Timer.prototype.play = function(reset) {
141
142        if (this.isReadyToStart()) {
143
144            if (reset) {
145                this.setTimer();
146            }
147            else {
148                this.setTimer(this.remaining);
149            }
150
151            this.isActive = true;
152
153        }
154
155        return this;
156
157    };
158
159    /**
160     * @returns {Timer}
161     */
162    Timer.prototype.pause = function() {
163
164        if (this.isActive) {
165
166            this.isActive   = false;
167            this.remaining -= new Date() - this.last;
168
169            this.clearTimer();
170
171        }
172
173        return this;
174
175    };
176
177    /**
178     * @returns {Timer}
179     */
180    Timer.prototype.stop = function() {
181
182        this.isActive  = false;
183        this.remaining = this.intervalTime;
184
185        this.clearTimer();
186
187        return this;
188
189    };
190
191    /**
192     * @param {boolean=} reset
193     * @returns {Timer}
194     */
195    Timer.prototype.toggle = function(reset) {
196
197        if (this.isActive) {
198            this.pause();
199        }
200        else if (reset) {
201            this.play(true);
202        }
203        else {
204            this.play();
205        }
206
207        return this;
208
209    };
210
211    /**
212     * @returns {Timer}
213     */
214    Timer.prototype.reset = function() {
215
216        this.isActive = false;
217        this.play(true);
218
219        return this;
220
221    };
222
223    /**
224     * @returns {Timer}
225     */
226    Timer.prototype.clearTimer = function() {
227        clearTimeout(this.timeoutObject);
228        return this;
229    };
230
231    /**
232     * @returns {Timer}
233     */
234    Timer.prototype.setTimer = function(time) {
235
236        var timer = this;
237
238        if (isNaN(time)) {
239            time = this.intervalTime;
240        }
241
242        this.remaining = time;
243        this.last      = new Date();
244
245        this.clearTimer();
246
247        this.timeoutObject = setTimeout(fnTimeout, time);
248
249        return this;
250
251        function fnTimeout() {
252            timer.execute();
253        }
254
255    };
256
257    /**
258     * @returns {Timer}
259     */
260    Timer.prototype.execute = function() {
261
262        if (this.isActive) {
263
264            try {
265                this.action();
266            }
267            finally {
268                this.setTimer();
269            }
270
271        }
272
273        return this;
274
275    };
276    
277})(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.