-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjTime.js
437 lines (341 loc) · 10.7 KB
/
jTime.js
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
/*
jTime: run your code when you want it to run.
cb: callback
tCb: trueCallback
fCb: falseCallback
startCb: startCallback
endCb: endCallback
actionObj = cb | { tcb: function, fCb: function }
*/
var jTime = (function() {
function JTime() {
this.SECOND = 1000;
this.MINUTE = 60 * this.SECOND;
this.HOUR = 60 * this.MINUTE;
this.DAY = 24 * this.HOUR;
this.WEEK = 7 * this.DAY;
this.DEFAULT_POLLING_INTERVAL = 100;
this.Poller = function() {
this.poller;
this.killMe = false;
this.isClear = false;
this.clear = function() {
this.killMe = true;
this.isClear = true;
clearTimeout(this.poller);
}
}
this.mySetInterval = function(period, callback, context) {
var start = new Date().getTime();
var time = 0;
var elapsed = '0.0';
// this sets p.killMe to false
var p = new t_.Poller();
function next()
{
if (p.killMe == false) {
callback.call(context);
time += period;
elapsed = Math.floor(time / period) / 10;
if (Math.round(elapsed) == elapsed) { elapsed += '.0'; };
var diff = (new Date().getTime() - start) - time;
if (p.killMe == false) { p.poller = setTimeout(next, period - diff) };
}
}
p.poller = setTimeout(next, period);
return p;
}
} // JTime()
var jTime = new JTime();
// aliases
var T_ = JTime;
JTime.prototype.clear = function(p) {
if (p)
p.clear();
}
JTime.prototype.setTimeout = function(timeout, callback) {
var p = new t_.Poller();
p.poller = setTimeout(callback, timeout);
return p;
}
JTime.prototype.setInterval = function(timeout, callback) {
var p = t_.mySetInterval(timeout, callback);
return p;
}
// polls for the first occurence of condition
JTime.prototype.becomes = function(pollInterval, condition, actionObj, context) {
var p;
if (typeof(actionObj.startCb) == "function") {
startCb.call(context);
};
p = t_.mySetInterval(pollInterval, function() {
if (condition()) {
if (typeof(actionObj) === "function") { actionObj.call(context); };
if (typeof(actionObj.tCb) === "function") { actionObj.tCb.call(context); };
t_.clear(p);
} else {
if (typeof(actionObj.fCb) === "function") { actionObj.fCb.call(context); };
}
}, context);
return p;
}
JTime.prototype.until = function(pollInterval, condition, callback, context) {
var p;
if (typeof(callback) == "function") {
p = t_.becomes(pollInterval, condition,
{ fCb: callback }, context);
};
return p;
}
JTime.prototype.when = function(pollInterval, timeout, condition, actionObj, context) {
var p;
var curTime = (new Date()).getTime();
if (typeof(actionObj.startCb) == "function") {
startCb.call(context);
};
p = t_.mySetInterval(pollInterval, function() {
if (condition()) {
if (typeof(actionObj) === "function") { actionObj.call(context); };
if (typeof(actionObj.tCb) === "function") { actionObj.tCb.call(context); };
} else {
if (typeof(actionObj.fCb) === "function") { actionObj.fCb.call(context); };
}
}, context);
setTimeout(function() {
t_.clear(p);
if (typeof(actionObj.endCb) === "function") {
actionObj.endCb.call(context);
}
},
timeout-curTime);
return p;
}
JTime.prototype.every = function(period, start, timeout, actionObj, context) {
var p;
var curTime = (new Date()).getTime();
if (timeout <= start) {
console.error("jTime.every() ERROR: timeout must be greater than start");
return;
};
if (typeof(actionObj.startCb) == "function") {
startCb.call(context);
};
setTimeout(function() {
/* call it right away when we start */
if (typeof(actionObj) === "function") {
actionObj.call(context);
} else if (typeof(actionObj.cb) === "function") {
actionObj.cb.call(context);
}
p = t_.mySetInterval(period, function() {
if (typeof(actionObj) === "function") {
actionObj.call(context);
};
if (typeof(actionObj.cb) === "function") { actionObj.cb.call(context); };
}, context);
}, start-curTime);
setTimeout(function() {
t_.clear(p);
if (typeof(actionObj.endCb) === "function") {
actionObj.endCb.call(context);
}
},
timeout-curTime);
return p;
}
JTime.prototype.byDeadline = function(pollInterval, deadline, condition, actionObj, context) {
var happened = false;
var p1 = t_.mySetInterval(function() {
if (condition()) {
happened = true;
if (typeof(actionObj) == "function") {
actionObj.call(context);
};
if (typeof(actionObj.tCb) == "function") {
actionObj.tCb.call(context);
};
clear(p1);
}
}, pollInterval);
var p2 = setTimeout(deadline, function() {
if (happened == false) {
if (typeof(actionObj.fCb) == "function") {
actionObj.fCb.call(context);
}
};
clear(p1);
});
return p1;
}
JTime.prototype.notByDeadline = function(pollInterval, deadline, condition, actionObj, context) {
var p = t_.byDeadline(pollInterval, deadline, condition,
{ tCb: actionObj.fCb, fCb: actionObj.tCb });
return p;
}
/* Fires based on whichever condition happen first
conditionsActionsMap: [{condition: f; action: actionObj}]
*/
JTime.prototype.happensFirst = function(pollInterval, conditionsActionsMap, context) {
var p = t_.mySetInterval(pollInterval, function() {
for (var cA in conditionsActionsMap) {
if (cA.condition()) {
if (typeof(cA.actionObj) == "function") {
cA.actionObj.call(context);
}
};
break;
}
});
return p;
};
// functions for temporal relationships between conditions
// allInOrder
// Run actionObj or tCb once if all the conditions are true in order
// Run fCb until all of the conditions have yet come true
// a condition can stop being true, and a previous condition can come
// true again, as long as each next condition is true sometime after previous
// condition is true
// n.b. this is different from conditions _becoming_ true in order
JTime.prototype.allInOrder = function(pollInterval, conditions, actionObj, context) {
var p = t_.mySetInterval(function() {
var i=0;
if (conditions[i]()) {
i++;
if (i >= conditions.length) {
if (typeof(actionObj) == "function") {
actionObj.call(context);
};
if (typeof(actionObj.tCb) == "function") {
actionObj.tCb.call(context);
};
clearInterval(p);
}
} else {
if (typeof(actionObj.fCb) == "function") {
actionObj.fCb.call(context);
}
}
}, pollInterval);
return p;
};
JTime.prototype.at = function(time, pollInterval, callback, context) {
var p = this.becomes(pollInterval,
function() {
var curDate = new Date();
return (curDate.getTime() >= time);
},
callback, context);
return p;
}
JTime.prototype.after = function(time, pollInterval, callback, context) {
var p = this.when(pollInterval,
time + 29999,
function() {
return ((new Date()).getTime() > time)
},
callback, context);
return p;
}
JTime.prototype.before = function(time, pollInterval, callback, context) {
var p = this.when(pollInterval,
time + 2999, // timeout after big 3 second fudge factor
function() {
return ((new Date()).getTime() < time)
},
callback, context);
return p;
}
// decorates function callback with a wrapper that prevents it
// from executing more than once in the interval
JTime.prototype.debounce = function(callback, interval, doLeading) {
var p = null, cachedResult; // shared by each invocation
return function() {
var higherThis = this, higherArguments = arguments;
var doNow = doLeading && (p === null);
t_.clear(p);
p = t_.setTimeout(interval, function() {
p = null;
if (!doLeading)
cachedResult = callback.apply(higherThis, higherArguments);
});
if (doNow)
cachedResult = callback.apply(higherThis, higherArguments);
return cachedResult;
}
}
JTime.prototype.Schedule = function() {
this.sched = new Array();
this.timeouts = new Array();
this.addTime = function(time) {
this.sched.push(time);
};
// doesn't matter if the array is sorted
this.setTimes = function(timeArray) {
this.sched = timeArray;
};
this.run = function(callback, context) {
for (i = 0; i<this.sched.length; i++) {
t_.at(this.sched[i], 100, callback, context);
};
};
}
JTime.prototype.timeIsWithin = function(min, max) {
var curTime = (new Date()).getTime();
return ((curTime > min) && (curTime < max));
}
JTime.prototype.longEach = function(pollInterval, array, step, callback, context) {
var p = t_.until(pollInterval,
function() { return (array.length === 0) },
function() {
for (var i=0; (i < step && array.length>0); i++) {
var item = array.shift();
callback.call(context, item);
}
});
return p;
}
JTime.prototype.now = function() {
return (new Date()).getTime();
}
JTime.prototype.nowChopped = function(modulus) {
if (modulus)
return t_.now() % modulus;
else
return t_.now() % 100000;
}
JTime.prototype.zeroPad = function(int) {
int = int.toString();
if (int.length > 1)
return int;
else
return "0" + int;
}
// get the starting second of the next minute, hour, day, or week
// n.b. these don't account for leap seconds
JTime.prototype.nextPasses = function(milliseconds) {
var now = t_.now();
return now + milliseconds - now % (milliseconds);
}
JTime.prototype.nextMinute = function() {
var now = t_.now();
return now + t_.oneMinute - now % (t_.oneMinute);
}
JTime.prototype.nextHour = function() {
var now = t_.now();
return now + t_.oneHour - now % (t_.oneHour);
}
JTime.prototype.nextDay = function() {
var now = t_.now();
return now + t_.oneDay - now % (t_.oneDay);
}
JTime.prototype.nextWeek = function() {
var now = t_.now();
return now + t_.oneWeek - now % (t_.oneWeek);
}
return jTime;
}());
var t_ = jTime; // alias
// exports for node.js
if (typeof module != 'undefined'){
module.exports = t_;
}