apigood first issue
Repository metrics
- Stars
- (2,250 個のスター)
- PR merge metrics
- (30d に merged PR はありません)
説明
I noticed that setInterval is not a thing in windowjs, so I decided to write my own implementation:
function* getIntervalIdgen() {
var i = 0;
yield i++;
}
const intervalIdGenerator = getIntervalIdgen();
function getId() {
return intervalIdGenerator.next.value;
}
{
var g;
if (typeof window !== "undefined") {
g = window;
} else if (typeof global !== "undefined") {
g = global;
} else if (typeof self !== "undefined") {
g = self;
} else {
g = this;
}
g.setInterval = function (callback, timer) {
let id = getId();
g.intervalState.intervals.push({
callback,
timer,
timeDelta: Date.now(),
id,
});
return id;
};
g.clearInterval = function (id) {
g.intervalState.intervals = g.intervalState.intervals.filter(function (
item
) {
return item.id != id;
});
};
g.intervalState = {};
g.intervalState.intervals = [];
function fireIntervals() {
requestAnimationFrame(fireIntervals);
for (const interval of g.intervalState.intervals) {
if (Date.now() - interval.timeDelta >= interval.timer) {
interval.callback();
interval.timeDelta = Date.now();
}
}
}
requestAnimationFrame(fireIntervals);
}
Would be nice if I could contribute that to the repo, but I don't want to mess with c.