This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 條件等待:等到指定元素出現 | |
function waitForElement(selector, timeout = 15000, interval = 300) { | |
return new Promise((resolve, reject) => { const end = Date.now() + timeout | |
;(function check() { | |
const el = document.querySelector(selector) | |
if (el) return resolve(el) | |
if (Date.now() > end) | |
return reject(new Error('元素等待超時: ' + selector)) | |
setTimeout(check, interval) | |
})() |