我正在使用jquery每x秒进行一次ajax调用。我还想知道的是,如何注册相同的Ajax函数以在页面加载时执行?jquery如何在页面加载时执行ajax
这里是我的AJAX功能看起来像(每30秒火灾)...
$().ready(function() {
// Poll for bulletin bar message
var url = $.url("/pollBulletin.htm");
pollTimer = setInterval(function() {
$.ajax({
url: url,
type: 'GET',
error: function(data) {
// The server may be down for the night or there may be a
// network blip. As such try to poll 10 times
// if still failing kill the poll.
retryCount = retryCount + 1;
if (pollTimer != null && retryCount >= maxRetries) {
clearInterval(pollTimer);
}
},
success: function(bulletinBarMessage) {
// Once we have a successful poll reset the retry count.
retryCount = 0;
var respContent = "";
respContent += bulletinBarMessage.messageLevel + " : ";
respContent += bulletinBarMessage.message;
$("#mt-news").html('
' + respContent + '');}
});
// When communication with the server is lost stop the poll.
}, pollInterval);
});
感谢
2014-03-26
Richie
+0
@Satpal ya它应该是甚至OP所使用的错误语法也会工作,因为准备好的承诺在内部使用jQuery –
+1
当谈到在间隔延迟上发出ajax请求时,请不要使用间隔,请考虑使用而是仅在请求完成后才调用新的调用。即使在这里,30secs应该足以让以前的请求完成,但是谁知道会发生什么...... –