Jquery ajax在Chrome中发回多个响应,而在FF中只发送一个响应

时间:2013-07-24 06:17:35

标签: jquery ajax django google-chrome

问题是,如果有新消息,而不是仅提醒一次,使用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("")

1 个答案:

答案 0 :(得分:0)

实际上,Firefox似乎是一个无法正常工作的人。请尝试使用以下代码:

def get_msg_notification(request):    
    while True:
        # check is there any new messages ?
        msg = Message.objects.filter(receiver = user, notify = False)
        if(patient < 5):
            time.sleep(5)
            patient+=1
        else:
            if (len(msg) != 0):
                msg.update(notify = True)
            break

    data = serializers.serialize('json', msg)
    return HttpResponse(data, mimetype="application/json")