【发布时间】:2013-07-23 11:43:02
【问题描述】:
问题是,当有新消息时,不是只提醒一次,而在 Chrome 中,它会以相同的值提醒 5-8 次。相比之下,这些代码与 FF 配合得很好,它只会发出一次警报。如何修复代码以支持 Chrome。 HTML:
setInterval(function() {
$.get('/restaurant/get_msg_notification',{},function(data){
alert(data)
})
}, 1000)
查看:
def get_msg_notification(request):
while True:
# check is there any new messages ?
msg = Message.objects.filter(receiver = user, notify = False)
if( len(msg)==0 and patient<5):
time.sleep(5)
patient+=1
else:
break
if( len(msg) > 0):
data = serializers.serialize('json', msg)
msg.update(notify = True)
return HttpResponse(data, mimetype="application/json")
else:
return HttpResponse("")
【问题讨论】:
-
现在我尝试删除“患者”变量,所以它根本不是长轮询。该方法给了我一个正确的结果(只响应一次)。但是,如果没有长轮询,ajax 需要每秒发送一个请求,这很昂贵。
-
从这个链接中找到了长轮询的很好的解释和解决方案。 techoctave.com/c7/posts/…
标签: jquery ajax django google-chrome