简体   繁体   中英

How to periodically retrieve page output and display to user with JavaScript?

I'm a JavaScript newbie and have no idea how to accomplish this, so a push (or shove!) in the right direction would be really great.

I have an HTML page and I need to display some data to a user that is constantly changing. The data is just text output from a web page.

What would be the best way to get the output of a page and then display that output to the user using JavaScript?

Thanks!

If you are trying to load content from another webpage for display on your webpage I would recommend using jQuery:

$(function() {
    setInterval(function() {
        $.get('url_here', function(res) {
            $('#result').html(res);
        });
    }, 30000);
});

This will load the load entire html response into a div like <div id="result"></div> for instance, and will perform this process every 30 seconds (30000 millisconds).

If you need to only display part of the html response you'll need to parse it out of res then switch the one line to something like $('#result').html(myValue); .

If you need the server to push to the client rather than the client pulling from the server then you probably want a different solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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