Update task.js

This commit is contained in:
Namhyeon Go 2021-06-23 14:54:47 +09:00 committed by GitHub
parent 4ece56e47e
commit 4382867eb8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -42,11 +42,21 @@ var TaskQueue = function() {
};
this.get = function() {
var task = null;
var now = new Date().getTime();
var delta = now;
try {
if (this.queue.length > 0) {
task = this.queue[0];
this.queue = this.queue.slice(1);
var k = 0;
for (var i = 0; i < this.queue.length; i++) {
var _delta = now - this.queue[i].when;
if (delta > _delta) {
k = i;
delta = _delta;
}
}
task = this.queue[k];
this.queue = this.queue.splice(k, 1);
}
} catch(e) {
console.error("TaskQueue.get: " + e.message);
@ -56,7 +66,7 @@ var TaskQueue = function() {
};
this.next = function() {
var result = null;
try {
var task = this.get();
@ -66,7 +76,7 @@ var TaskQueue = function() {
} catch (e) {
console.error("Task exception: " + e.message);
}
if (task.nextTask != null) {
this.put(task.nextTask);
}