function waitFor_elementById (emlId: string) {
return new Promise(resolve => {
const $el = document.getElementById(emlId)
if ($el) return resolve($el)
const observer = new MutationObserver(mutations => {
const $el = document.getElementById(emlId)
if ($el) {
resolve($el)
observer.disconnect()
}
})
observer.observe(document.body, {
childList: true,
subtree: true
})
})
}
จะใช้แบบ await
หรือจะ then
ก็ได้
await waitFor_elementById('elm_id')
// OR
waitFor_elementById('elm_id').then(() => {
// ...
})
และจริงๆ ไม่ต้องใช้ document.getElementById
ก็ได้ ไปใช้ อย่างอื่น ที่ต้องการหา เช่น