mirror of
https://github.com/ajayyy/SponsorBlock.git
synced 2026-01-30 06:10:55 +03:00
Improved await function.
This commit is contained in:
13
utils.js
13
utils.js
@@ -1,14 +1,20 @@
|
|||||||
// Function that can be used to wait for a condition before returning
|
// Function that can be used to wait for a condition before returning
|
||||||
async function wait(condition, timeout = 5000, check = 100) {
|
async function wait(condition, timeout = 5000, check = 100) {
|
||||||
return await new Promise((resolve, reject) => {
|
return await new Promise((resolve, reject) => {
|
||||||
setTimeout(() => {reject("TIMEOUT")}, timeout);
|
setTimeout(() => reject("TIMEOUT"), timeout);
|
||||||
const interval = setInterval(() => {
|
|
||||||
|
let intervalCheck = () => {
|
||||||
let result = condition();
|
let result = condition();
|
||||||
if (result !== false) {
|
if (result !== false) {
|
||||||
resolve(result);
|
resolve(result);
|
||||||
clearInterval(interval);
|
clearInterval(interval);
|
||||||
};
|
};
|
||||||
}, check);
|
};
|
||||||
|
|
||||||
|
let interval = setInterval(intervalCheck, check);
|
||||||
|
|
||||||
|
//run the check once first, this speeds it up a lot
|
||||||
|
intervalCheck();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -37,5 +43,6 @@ function getYouTubeVideoID(url) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user