开发者

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

I'm currently programming in JSP and 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). 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开发者_StackOverflow中文版.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).

Any help would be appreciated,

Thanks.


The JSP code within the <% ... %> tags runs only once, on the server-side, when the page is loaded. 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.

To update the data as you are expecting, you can use 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 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. Finally HTML/CSS/JS runs at the webbrowser.

If you rightclick the page in webbrowser and choose View Source, you'll probably understand what I mean. There's no single line of Java/JSP code. It has already done its job of generating the HTML/CSS/JS. The only communication way between Java/JSP and JavaScript is HTTP.

You need to move this job to some servlet in the server side and let JS invoke this asynchronously ("in the background"). This is also known as "Ajax". Here's a kickoff example with a little help of 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:

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

See also:

  • Communication between Java/JSP/JSF and JavaScript
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜