Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我们如何使用Javascript创建实时提要?_Javascript_Jsp - Fatal编程技术网

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

我们如何使用Javascript创建实时提要?,javascript,jsp,Javascript,Jsp,我目前正在用JSP和Javascript编程。(我决不是这两方面的专家)。现在,我想要的是重复调用Javascript函数,并从数据库中重复查询其中一个变量(这是页面最后一次修改的日期)。如果此变量大于页面加载时的值,我希望页面刷新 到目前为止,我所拥有的: ... <body onload="Javascript:refreshMethod()"> <script type="text/JavaScript"> <!-- function refreshMetho

我目前正在用JSP和Javascript编程。(我决不是这两方面的专家)。现在,我想要的是重复调用Javascript函数,并从数据库中重复查询其中一个变量(这是页面最后一次修改的日期)。如果此变量大于页面加载时的值,我希望页面刷新

到目前为止,我所拥有的:

...
<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>
。。。
问题是每次timedRefresh运行时,FeedLastModifiedIn都不会更改(即使它已更改)

任何帮助都将不胜感激


谢谢。

加载页面时,
标记中的JSP代码只在服务器端运行一次。如果您在浏览器中查看页面的源代码,您会发现这些值已经放在JavaScript代码中,因此它们在每个计时器间隔期间都不会更改


要按预期更新数据,可以使用。您可以在网上找到大量教程。

JSP和JavaScript并不像您从编码中期望的那样同步运行。JSP在Web服务器上运行,生成一组字符,这些字符应继续作为HTML/CSS/JS,Web服务器将其作为HTTP响应发送给webbrowser,作为对webbrowser发起的HTTP请求的响应。最后,HTML/CSS/JS在webbrowser上运行

如果您右键单击webbrowser中的页面并选择View Source,您可能会理解我的意思。没有一行Java/JSP代码。它已经完成了生成HTML/CSS/JS的工作。Java/JSP和JavaScript之间的唯一通信方式是HTTP

您需要将此作业移动到服务器端的某个servlet,并让JS异步(“在后台”)调用此作业。这也称为“Ajax”。下面是一个有点帮助的启动示例

另见:

谢谢您的帮助。还有一件事。您的代码工作正常,但我想知道是否有办法使JQuery脚本在加载页面时暂停执行。否则,当页面仍在加载过程中时,它会不断发送刷新操作。在响应标题出现之前,它真的需要10秒以上吗?无论如何,我已经编辑了这个示例,以便在刷新页面时清除间隔。
<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>
response.setContentType("application/json");
if (EventManager.getFeedLastModified(eventID) > currentTime) {
    response.getWriter().write("true");
} else {
    response.getWriter().write("false");
}