setTimeout
Same issue for looping with setTimeout
setTimeout + await async
Same issue for looping with async delay
while(true){
await new Promise(r=> setTimeout(r,1));
let t = window._yt_player;
if(t) break;
}
requestAnimationFrame
Although replacing by requestAnimationFrame
would help, but the delay would be much longer which violates the original aim; and cannot work in background tab (reloading in background, via DevTools)
requestIdleCallback + timeout
- Same as requestAnimationFrame
(async () => {
while(true){
await new Promise(r=> requestIdleCallback(r,{timeout:1}));
let t = window._yt_player;
if(t) break;
}
let t = window._yt_player;
console.log('_yt_player is found', !!_yt_player)
})();