简体   繁体   English

无需在网页上创建更多元素即可更新 HTML 数据

[英]Update HTML data without creating more elements on the webpage

Have an interval that loads the html every 3 secs, but want to refresh it and not keep adding more under the already made code.有一个每 3 秒加载一次 html 的间隔,但想要刷新它而不是在已经制作的代码下继续添加更多内容。

async function Products(){
            setInterval(async function() {
                const response = await fetch('http://localhost:3000/api/products');
                const data = await response.json();

                const mainContainer = document.getElementById("myData");
                for (let obj of data) {
                    const div = document.createElement("div");
                    div.innerHTML = `${obj["sku"]}: ${obj["name"]}`;
                    mainContainer.appendChild(div);
                }
            }, 10000)
        }

When I click a start button everything works, but how do i make it refresh the already made HTML rather than repeatedly recreating it with the interval.当我单击开始按钮时一切正常,但是我如何让它刷新已经制作的 HTML,而不是用间隔反复重新创建它。 Trying to figure out a good approach to this.试图找出一个好的方法来解决这个问题。 Thanks谢谢

Create and append a <div> immediately, and in the interval, assign to its innerHTML :立即创建并附加一个<div> ,并在间隔中分配给其innerHTML

function Products() {
    const container = document.getElementById("myData").appendChild(document.createElement("div"));
    setInterval(async function () {
        const response = await fetch('http://localhost:3000/api/products');
        const data = await response.json();
        container.innerHTML = '';
        for (let obj of data) {
            container.innerHTML += `${obj["sku"]}: ${obj["name"]}`;
        }
    }, 10000)
}

I think what you are trying to do is implement a Serie of observable elements that you can look only for those who have changed instead of all the data, something like React does with the virtual DOM.我认为您想要做的是实现一系列可观察元素,您只能查找那些已更改的元素而不是所有数据,就像 React 对虚拟 DOM 所做的那样。

Considering the code tout already posted, refreshing elements at a set interval is a bad idea.考虑到已经发布的代码,以设定的时间间隔刷新元素是一个坏主意。 What if you have 1000 user refreshing at the same time?如果您有 1000 个用户同时刷新怎么办? What if it cause your response tone to be more then 3 seconds?如果它导致您的响应音超过 3 秒怎么办?

That said, if you really want to work on creating something like that, you have to find a way to load not all the products from the api, but only the ones that are different.也就是说,如果你真的想创建类似的东西,你必须找到一种方法,不是从 api 加载所有产品,而是只加载不同的产品。 Again, if you want to keep it that way, here are, in my opinion, what you could do:同样,如果您想保持这种状态,在我看来,您可以这样做:

  1. Start by loading all the product on the page, but set an interval to check a new Endpoint which would tell you what products have been added after the last one.首先加载页面上的所有产品,但设置一个时间间隔来检查一个新的端点,它会告诉您在最后一个之后添加了哪些产品。

  2. Use either an index or a key to identify which product is which, so tout know the ones you have.使用索引或键来识别哪个产品是哪个,所以要知道你拥有的产品。

  3. You need a way to know quick product was updated since the last load.您需要一种方法来了解自上次加载以来快速更新的产品。

That's a start.那是一个开始。 You can implement these in different way.您可以以不同的方式实现这些。 I would suggest having a timestamp for the time created and updated, so you can then query only those who fall after this timestamp.我建议为创建和更新的时间设置一个时间戳,这样你就可以只查询那些落在这个时间戳之后的人。

Add a dynamic ID to your product elements, (eg <div id=${sku} >**Product Here**<\\div> That way, you can track your product and recreate only the ones who changed/are new.为您的产品元素添加动态 ID,(例如<div id=${sku} >**Product Here**<\\div>这样,您可以跟踪您的产品并仅重新创建更改/新产品的产品。

That's obviously a complicated way of implementing an open connection, if you want another solution, you could also open a Socket with your api, which would send event for data updated and created and would ultimately make your 3 second ping obsolete, resulting in a better scalability in my opinion.这显然是一种实现开放连接的复杂方法,如果您想要其他解决方案,您也可以使用您的 api 打开一个 Socket,这将发送数据更新和创建的事件,并最终使您的 3 秒 ping 过时,从而产生更好的效果我认为可扩展性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM