如何阻止浏览器“等待”用于 Node.js 回调
我正在 Node.js 上创建一个 Express JS 应用程序,并实现了一个页面,该页面将根据 chat.nodejs.org (https://github.com/ry/node_chat) 回调实时更新。我已按照“node_chat”中的方法进行操作,如果从其他浏览器进行更新,页面会正确更新。唯一的问题是我似乎无法阻止浏览器(Chrome)“等待本地主机”并旋转,我认为它不需要这样做。
这是我的代码的简化版本(为了简洁)。有什么想法吗?
client.js(客户端):
function poll(data) {
// ...
if(data){
// do something with the data
}
$.ajax({
cache:false,
type:'GET',
url:'/json-people',
dataType: 'json',
data: {
slug: queue.slug,
since: CONFIG.last_timestamp
},
error: function () {
// handle error
},
success: function (data) {
poll(data);
}
});
};
app.js:
app.get('/json-people', function (req, res) {
queueProvider.query(req.param('slug'), req.param('since'), function (people){
res.send({people: people}, 200);
});
});
我在queueProvider.query方法中放入什么似乎并不重要,它会旋转直到执行res.send(...)。
我使用 https://github.com/ry/node_chat/blob/master/client .js 和 https://github.com/ry/node_chat/blob/master/server。 js供参考。
特别是:
fu.get("/recv", function (req, res) {...}
和
channel.query()
,
channel.appendMessage()
谢谢。
I am creating an Express JS app on Node.js and have implemented a page that will callback for live updates as per chat.nodejs.org (https://github.com/ry/node_chat). I've followed the approach in 'node_chat' to a t, and the page updates correctly if an update is made from another browser. The only problem is I can't seem to stop the browser (Chrome) from 'Waiting for localhost' and spinning away, which I don't think it needs to do.
Here's simplified versions of my code (for brevity). Any ideas?
client.js (client side):
function poll(data) {
// ...
if(data){
// do something with the data
}
$.ajax({
cache:false,
type:'GET',
url:'/json-people',
dataType: 'json',
data: {
slug: queue.slug,
since: CONFIG.last_timestamp
},
error: function () {
// handle error
},
success: function (data) {
poll(data);
}
});
};
app.js:
app.get('/json-people', function (req, res) {
queueProvider.query(req.param('slug'), req.param('since'), function (people){
res.send({people: people}, 200);
});
});
it doesn't seem to matter what I put in the queueProvider.query method, it will spin away until res.send(...) is executed.
I used https://github.com/ry/node_chat/blob/master/client.js
and https://github.com/ry/node_chat/blob/master/server.js for reference.
Particularily:
fu.get("/recv", function (req, res) {...}
and
channel.query()
and
channel.appendMessage()
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议为了避免出现这个旋转图标,您可以从轮询切换到 WebSocket 之类的方式(例如 http://socket.io )
I would recommend that to avoid this spinning icon, you switch from polling to something like WebSockets (e.g. http://socket.io)