我在异步里面调document.getElementById("remoteVideo");获取不到方法,remoteVideo在弹窗里面,弹窗已经出来了,但是获取不到方法
时间: 2024-03-09 20:47:43 浏览: 22
如果remoteVideo元素在弹窗中,并且弹窗是通过异步方式加载的,那么你需要等待弹窗完全加载完成后才能获取remoteVideo元素。你可以使用Promise或者回调函数来实现异步加载完成后的回调。具体实现方式可能因你的代码结构而异,以下是一些可能的示例:
使用Promise:
```
function waitForElementToDisplay(selector) {
return new Promise((resolve, reject) => {
const interval = setInterval(() => {
const remoteVideo = document.querySelector(selector);
if (remoteVideo) {
clearInterval(interval);
resolve(remoteVideo);
}
}, 100);
});
}
// 在异步函数中使用
async function myAsyncFunction() {
// 异步加载弹窗
await loadPopup();
// 等待remoteVideo加载完成
const remoteVideo = await waitForElementToDisplay('#remoteVideo');
// 这里可以使用remoteVideo元素了
console.log(remoteVideo);
}
```
使用回调函数:
```
function waitForElementToDisplay(selector, callback) {
const interval = setInterval(() => {
const remoteVideo = document.querySelector(selector);
if (remoteVideo) {
clearInterval(interval);
callback(remoteVideo);
}
}, 100);
}
// 在异步函数中使用
function myAsyncFunction() {
// 异步加载弹窗
loadPopup().then(() => {
// 等待remoteVideo加载完成
waitForElementToDisplay('#remoteVideo', (remoteVideo) => {
// 这里可以使用remoteVideo元素了
console.log(remoteVideo);
});
});
}
```
需要注意的是,这些示例都是用了定时器轮询的方式来等待元素加载完成,因此需要设置一个合适的轮询间隔时间,以免对页面性能产生过大的影响。另外,如果页面元素的加载方式有所变化,这些示例可能需要做一些调整。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)