【问题标题】:Poll an Express server with $.get regularly定期使用 $.get 轮询 Express 服务器
【发布时间】:2016-09-01 11:28:48
【问题描述】:

有一个带有服务器时间生成器的 Express 服务器。生成的时间发送到客户端页面gtm.hbs。如何从客户端页面定期轮询服务器?我正在寻找一个 ajax 解决方案。

这是存储在\routes\getTime.js中的服务器端代码:

module.exports = {  
    sendCurrentUTCTimeString: function(req, res, next) {
        var currentUTCTime = new Date();
        var currentUTCHours = currentUTCTime.getUTCHours();
        var currentUTCMinutes = currentUTCTime.getUTCMinutes();
        var currentUTCSeconds = currentUTCTime.getUTCSeconds();

        currentUTCHours = ( currentUTCHours < 10 ? "0" : "" ) + currentUTCHours;
        currentUTCMinutes = ( currentUTCMinutes < 10 ? "0" : "" ) + currentUTCMinutes;
        currentUTCSeconds = ( currentUTCSeconds < 10 ? "0" : "" ) + currentUTCSeconds;
        currentUTCHours = ( currentUTCHours == 24 ) ? "00" : currentUTCHours;

        function pad(number, length) {
            var str = "" + number;
            while (str.length < length) {
                str = '0' + str;
            }
            return str;
        }

        var offset = new Date().getTimezoneOffset();
        offset = ((offset < 0 ? '+' : '-') + pad(parseInt(Math.abs(offset / 60)), 2) + pad(Math.abs(offset % 60), 2));
        offset = offset.match(/[\+-][0-9][0-9]/)[0];
        res.send({currentUTCTime: currentUTCTime, currentUTCHours: currentUTCHours, currentUTCMinutes: currentUTCMinutes, currentUTCSeconds:currentUTCSeconds, offset: offset});
    }
}

gettime.hbs 页面代码是

<script>
    $(document).ready(function() {
        setInterval(function() {
            var currentUTCHours;
            var currentUTCMinutes;
            var currentUTCSeconds;
            var offset;
            $.get('/gettime', function(data) {
                debugger;
                console.log(data);
                currentUTCHours = data.currentUTCHours;
                currentUTCMinutes = data.currentUTCSeconds;
                currentUTCSeconds = data.currentUTCMinutes;
                offset = data.offset;
                $('#currentUTCHours').text(currentUTCHours);
                $('#currentUTCMinutes').text(currentUTCMinutes);
                $('#currentUTCSeconds').text(currentUTCSeconds);
                $('#offset').text(offset);
            });

            console.log('currentUTCHours: ' + currentUTCHours);
        }, 1500);
    });
</script>

currentUTCHours: <span id="currentUTCHours"></span><br>
currentUTCMinutes: <span id="currentUTCMinutes"></span><br>
currentUTCSeconds: <span id="currentUTCSeconds"></span><br>
offset: <span id="offset"></span><br>

在这里,我想呈现一个页面,该页面将轮询服务器,然后用服务器响应值填充四个 &lt;span&gt;s。

连接服务器和客户端的路由是

app.get('/gettime', getTime.sendCurrentUTCTimeString);
app.get('/gtm', function(req, res, next) {
    res.render('gettime', {});
}); 

【问题讨论】:

  • 每个服务器都会在每个响应中发送一个Date 标头。我建议您使用该标头值而不是滚动您自己的机制。
  • 在 Express 应用程序中定期轮询服务器似乎是一种奇怪的方式,因为您可以很容易地使用 Socket.io 将更改推送到客户端。
  • 同意。然而,这是我需要解决这个问题的方式。我确实会在明天发布解决方案。
  • 把上面的解决方案贴出来,以防其他人有用。

标签: javascript ajax node.js express


【解决方案1】:
$('#currentUTCHours').text(currentUTCHours);
$('#currentUTCSeconds').text(currentUTCSeconds);

应该在 $.get() 中以使更改可见。

【讨论】:

    猜你喜欢
    • 2012-07-31
    • 2015-12-31
    • 1970-01-01
    • 2015-04-06
    • 1970-01-01
    • 2014-08-24
    • 1970-01-01
    • 2019-06-13
    • 2021-12-29
    相关资源
    最近更新 更多