I have implemented comet client in jquery as following:
$(docume开发者_如何学JAVAnt).ready(function () {
comet();
});
function comet(){
var cometJSON = {
'comet': 'id'
}
$.ajax({
type: "POST",
url: "http://localhost:8080/comet",
data: JSON.stringify(cometJSON),
async: true, /* Set async request*/
cache: false,
timeout:50000, /* Timeout in ms */
success: function(data){
console.log('suc');
eventReceived(data);
},
error: function(jqXHR, textStatus, errorThrown){
console.log("error: "+textStatus);
},
complete: function(jqXHR, textStatus){
console.log("Send new comet!");
comet();
}
});
};
Everything works fine, but I have always noisy spinner in my browser tab and my status pannel always shows: Waiting for localhost, how can I fix that?
The spinner indicates a connection in progress, which is exactly what is happening - after you receive an answer, in complete
section you instantly trigger a new request, hence there is a connection in progress most of the time (pretty much always). To avoid it, you need to do a delay before a new request - setTimeout(comet, 1000)
sounds like a good alternative to the last comet();
I implemented the same thing a while back and ran into the same problem:
https://github.com/tenorviol/cometjax
To solve the problem of the forever spinner, put a timeout on your initial ajax call, e.g.:
$(document).ready(function () {
setTimeout(function() {
comet();
}, 10);
});
精彩评论