英文:
Is it possible to await a function that doesn't return anything
问题
我有一个for循环,它从另一个文件中获取代码,并使用insertAdjacentHTML()
方法将其附加到我的网站。之后,它会修改新添加的代码片段的innerHTML。我的当前解决方案看起来像这样:
for (let i = 0; i < 3; i++) {
fetch("myFile.html")
.then((obj) => obj.text())
.then((text) => {
document.getElementById("addContentHere").insertAdjacentHTML("beforeend", text);
document.getElementsByClassName("elementInAddedContent")[i].innerHTML = "Modified content";
});
}
在我的网站上,我有以下代码:
<div id="addContentHere"></div>
我要获取的HTML大致如下:
<div class="addedElement">
<div class="elementInAddedContent"></div>
</div>
因此,我的网站最终结果应该如下所示:
<div id="addContentHere">
<div class="addedElement">
<div class="elementInAddedContent">Modified content</div>
</div>
<div class="addedElement">
<div class="elementInAddedContent">Modified content</div>
</div>
<div class="addedElement">
<div class="elementInAddedContent">Modified content</div>
</div>
</div>
当然,所有添加的元素都将具有不同的内容,但出于示例目的,我保持了简单。问题是计算机在元素添加之前就到达了document.getElementsByClassName("elementInAddedContent")[i].innerHTML = "Modified content";
这一行,因此我会收到Uncaught (in promise) TypeError: Cannot set properties of undefined (setting 'innerHTML')
错误。我是否可以在不返回任何内容的代码上使用await
?就像这样:
for (let i = 0; i < 3; i++) {
fetch("myFile.html")
.then((obj) => obj.text())
.then(async (text) => {
await document.getElementById("addContentHere").insertAdjacentHTML("beforeend", text);
document.getElementsByClassName("elementInAddedContent")[i].innerHTML = "Modified content";
});
}
英文:
I have a for loop that fetches code from another file and appends it to my website using the insertAdjacentHTML()
method. After that it modifies the innerHTML of the freshly added piece of code. My current solution looks something like this:
for (let i = 0; i < 3; i++) {
fetch("myFile.html")
.then((obj) => obj.text())
.then((text) => {
document.getElementById("addContentHere").insertAdjacentHTML("beforeend", text);
document.getElementsByClassName("elementInAddedContent")[i].innerHTML = "Modified content";
});
}
In my website I have the following code:
<div id="addContentHere"></div>
And the HTML that I'm fetching is something like this:
<div class="addedElement">
<div class="elementInAddedContent"></div>
</div>
So the end result in my website should look like this:
<div id="addContentHere">
<div class="addedElement">
<div class="elementInAddedContent">Modified content</div>
</div>
<div class="addedElement">
<div class="elementInAddedContent">Modified content</div>
</div>
<div class="addedElement">
<div class="elementInAddedContent">Modified content</div>
</div>
</div>
Of course all added elements are going to have different content, but for example purpouses I'm keeping it simple. The problem is that the computer reaches the document.getElementsByClassName("elementInAddedContent")[i].innerHTML = "Modified content";
before the element has been added and so I get the Uncaught (in promise) TypeError: Cannot set properties of undefined (setting 'innerHTML')
error. Can I somehow use await on code that doesn't itself return anything? So something like this:
for (let i = 0; i < 3; i++) {
fetch("myFile.html")
.then((obj) => obj.text())
.then((text) => {
await document.getElementById("addContentHere").insertAdjacentHTML("beforeend", text);
document.getElementsByClassName("elementInAddedContent")[i].innerHTML = "Modified content";
});
}
答案1
得分: 1
Your loop is trying as fast as possible to read a file, update the DOM and read again and update again. There is no control over what is happening since Fetch is async. I still do not understand why you had a loop around getting ONE file..
Perhaps you meant to do this?
I give the DOM 100 ms to be updated
fetch("myFile.html")
.then((obj) => obj.text())
.then((text) => {
document.getElementById("addContentHere").insertAdjacentHTML("beforeend", text);
setTimeout(() => document.querySelectorAll(".elementInAddedContent").forEach(el => el.textContent = "Modified content"), 100);
});
英文:
Your loop is trying as fast as possible to read a file, update the DOM and read again and update again. There is no control over what is happening since Fetch is async. I still do not understand why you had a loop around getting ONE file..
Perhaps you meant to do this?
I give the DOM 100 ms to be updated
<!-- language: lang-js -->
fetch("myFile.html")
.then((obj) => obj.text())
.then((text) => {
document.getElementById("addContentHere").insertAdjacentHTML("beforeend", text);
setTimeout(() => document.querySelectorAll(".elementInAddedContent").forEach(el => el.textContent = "Modified content"), 100);
});
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论