1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-01-02 12:47:41 +02:00
joplin/ReactNativeClient/lib/poor-man-intervals.js
2017-07-22 18:35:39 +01:00

50 lines
1.1 KiB
JavaScript

import { time } from 'lib/time-utils.js';
class PoorManIntervals {
static setInterval(callback, interval) {
PoorManIntervals.intervalId_++;
PoorManIntervals.intervals_.push({
id: PoorManIntervals.intervalId_,
callback: callback,
interval: interval,
lastIntervalTime: time.unixMs(),
});
return PoorManIntervals.intervalId_;
}
static intervalById(id) {
for (let i = 0; i < PoorManIntervals.intervals_.length; i++) {
if (PoorManIntervals.intervals_[i].id == id) return PoorManIntervals.intervals_[id];
}
return null;
}
static clearInterval(id) {
for (let i = 0; i < PoorManIntervals.intervals_.length; i++) {
if (PoorManIntervals.intervals_[i].id == id) {
PoorManIntervals.intervals_.splice(i, 1);
break;
}
}
}
static update() {
for (let i = 0; i < PoorManIntervals.intervals_.length; i++) {
let interval = PoorManIntervals.intervals_[i];
const now = time.unixMs();
if (now - interval.lastIntervalTime >= interval.interval) {
interval.lastIntervalTime = now;
interval.callback();
}
}
}
}
PoorManIntervals.intervalId_ = 0;
PoorManIntervals.intervals_ = [];
export { PoorManIntervals }