[英]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. 我是一个JavaScript新手,不知道如何实现这一点,所以在正确的方向上推(或推!)真的很棒。
I have an HTML page and I need to display some data to a user that is constantly changing. 我有一个HTML页面,我需要向不断变化的用户显示一些数据。 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? 获取页面输出然后使用JavaScript向用户显示输出的最佳方法是什么?
Thanks! 谢谢!
If you are trying to load content from another webpage for display on your webpage I would recommend using jQuery: 如果您尝试从其他网页加载内容以在您的网页上显示,我建议您使用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). 这将加载整个html响应加载到例如
<div id="result"></div>
的<div id="result"></div>
,并将每30秒(30000毫秒)执行此过程。
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);
如果您只需要显示部分html响应,则需要将其解析为
res
然后将一行切换为$('#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. 如果您需要服务器推送到客户端而不是客户端从服务器拉出,那么您可能需要一个不同的解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.