进页面马上执行$.ajax,jquery如何在页面加载时执行ajax

我正在使用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应该足以让以前的请求完成,但是谁知道会发生什么...... –

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值