简体   繁体   English

我们如何使用Javascript创建实时Feed?

[英]How would we use Javascript to create a real-time feed?

I'm currently programming in JSP and Javascript. 我目前正在使用JSP和Javascript进行编程。 (I am by no means an expert in either). (我也不是任何一位专家)。 Right now, what I want is for a Javascript function to be called repeatedly and one of the variables to be queried from the database repeatedly (it is the date that the page was last modified). 现在,我想要的是重复调用Javascript函数,并重复从数据库中查询其中一个变量(它是上次修改页面的日期)。 If this variable is greater than when the page was loaded, I want the page to refresh. 如果此变量大于加载页面的时间,我希望页面刷新。

What I have so far: 到目前为止我所拥有的:

...
<body onload="Javascript:refreshMethod()">
<script type="text/JavaScript">
<!--
function refreshMethod()
    {
     var interval = setInterval("timedRefresh()", 10000);
    }
function timedRefresh() {
 var currenttime = '<%=currentTime%>';
 var feedlastmodified = '<%=EventManager.getFeedLastModified(eventID)%>';
 var currenttimeint = parseInt(currenttime);
 var feedlastmodifiedint = parseInt(feedlastmodified);
 if(feedlastmodifiedint > currenttimeint)
 {
  alert(feedlastmodifiedint);
  setTimeout("location.reload(true);",timeoutPeriod);
 }
 if(feedlastmodifiedint < currenttimeint)
 {
  alert(feedlastmodifiedint + " : " + currenttimeint);
 }
}
//   -->


</script>

The problem is that everytime the timedRefresh runs, the feedlastModifiedInt never changes (even if it has been changed). 问题是每次timedRefresh运行时,feedlastModifiedInt都不会改变(即使它已被更改)。

Any help would be appreciated, 任何帮助,将不胜感激,

Thanks. 谢谢。

The JSP code within the <% ... %> tags runs only once, on the server-side, when the page is loaded. 装入页面时, <% ... %>标记内的JSP代码仅在服务器端运行一次。 If you look at the source of the page in the browser, you will find that these values have already been placed within the JavaScript code, and thus they will not change during each timer interval. 如果在浏览器中查看页面的源,您会发现这些值已经放置在JavaScript代码中,因此在每个计时器间隔内它们都不会改变。

To update the data as you are expecting, you can use AJAX . 要按预期更新数据,可以使用AJAX You can find plenty of tutorials online. 您可以在线找到大量教程。

JSP and JavaScript doesn't run in sync as you seem to expect from the coding. JSP和JavaScript不会像您期望的那样同步运行。 JSP runs at webserver, produces a bunch of characters which should continue as HTML/CSS/JS and the webserver sends it as a HTTP response to the webbrowser as response to a HTTP request initiated by the webbrowser. JSP在webserver上运行,产生一堆字符,这些字符应该继续作为HTML / CSS / JS,并且web服务器将它作为HTTP响应发送到webbrowser,作为对webbrowser发起的HTTP请求的响应。 Finally HTML/CSS/JS runs at the webbrowser. 最后,HTML / CSS / JS在webbrowser上运行。

If you rightclick the page in webbrowser and choose View Source , you'll probably understand what I mean. 如果右键单击Web浏览器中的页面并选择查看源 ,您可能会理解我的意思。 There's no single line of Java/JSP code. 没有单行的Java / JSP代码。 It has already done its job of generating the HTML/CSS/JS. 它已经完成了生成HTML / CSS / JS的工作。 The only communication way between Java/JSP and JavaScript is HTTP. Java / JSP和JavaScript之间唯一的通信方式是HTTP。

You need to move this job to some servlet in the server side and let JS invoke this asynchronously ("in the background"). 您需要将此作业移动到服务器端的某个servlet,并让JS异步调用它(“在后台”)。 This is also known as "Ajax". 这也称为“Ajax”。 Here's a kickoff example with a little help of jQuery . 这是在jQuery的帮助下的启动示例。

<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script>
    $(document).ready(function() {
        var refreshInterval = setInterval(function() {
            $.getJSON('refreshServlet', function(refresh) {
                if (refresh) {
                    clearInterval(refreshInterval);
                    location.reload(true);
                }
            });
        }, 10000);
    });
</script>

Where the doGet() method of the servlet which is mapped on an url-pattern of /refreshServlet roughly look like this: /refreshServleturl-pattern上映射的servlet的doGet()方法大致如下所示:

response.setContentType("application/json");
if (EventManager.getFeedLastModified(eventID) > currentTime) {
    response.getWriter().write("true");
} else {
    response.getWriter().write("false");
}

See also: 也可以看看:

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

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