jquery如何在页面加载时执行ajax

jquery如何在页面加载时执行ajax,jquery,ajax,Jquery,Ajax,我使用jquery每x秒进行一次ajax调用。我还想知道的是,如何注册相同的ajax函数以在页面加载时执行 下面是我的ajax函数的样子(每30秒启动一次) $().ready(函数()){ //轮询公告栏消息 var url=$.url(“/pollbuntribution.htm”); pollTimer=setInterval(函数(){ $.ajax({ url:url, 键入:“GET”, 错误:函数(数据){ //服务器可能当晚停机,或者可能出现故障 //网络漏洞。因此,尝试轮询10

我使用jquery每x秒进行一次ajax调用。我还想知道的是,如何注册相同的ajax函数以在页面加载时执行

下面是我的ajax函数的样子(每30秒启动一次)

$().ready(函数()){
//轮询公告栏消息
var url=$.url(“/pollbuntribution.htm”);
pollTimer=setInterval(函数(){
$.ajax({
url:url,
键入:“GET”,
错误:函数(数据){
//服务器可能当晚停机,或者可能出现故障
//网络漏洞。因此,尝试轮询10次
//如果仍然失败,取消投票。
retryCount=retryCount+1;
if(pollTimer!=null&&retryCount>=maxRetries){
清除间隔(轮询计时器);
}
},
成功:功能(bulletinBarMessage){
//一旦轮询成功,请重置重试计数。
retryCount=0;
var respContent=“”;
respContent+=bulletinBarMessage.messageLevel+“:”;
respContent+=bulletinBarMessage.message;
$(“#mt news”).html(“
    ”); } }); //当与服务器的通信中断时,停止轮询。 },Poll间期); });

    谢谢

    您应该将代码放入document.ready中,如下所示

    $(document).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('<ul><a href="#" target="_self">' + respContent + '</a></ul>');
                }
            });
            // When communication with the server is lost stop the poll.
            }, pollInterval);
    });
    
    $(文档).ready(函数(){
    //轮询公告栏消息
    var url=$.url(“/pollbuntribution.htm”);
    pollTimer=setInterval(函数(){
    $.ajax({
    url:url,
    键入:“GET”,
    错误:函数(数据){
    //服务器可能当晚停机,或者可能出现故障
    //网络漏洞。因此,尝试轮询10次
    //如果仍然失败,取消投票。
    retryCount=retryCount+1;
    if(pollTimer!=null&&retryCount>=maxRetries){
    清除间隔(轮询计时器);
    }
    },
    成功:功能(bulletinBarMessage){
    //一旦轮询成功,请重置重试计数。
    retryCount=0;
    var respContent=“”;
    respContent+=bulletinBarMessage.messageLevel+“:”;
    respContent+=bulletinBarMessage.message;
    $(“#mt news”).html(“
      ”); } }); //当与服务器的通信中断时,停止轮询。 },Poll间期); });
      试试这个

      $(document).ready(function(){
      
          makeAjaxCall();
      
          setInterval(function() {
                // Do something every 30 seconds
                makeAjaxCall();
          }, 30000);
      
          function makeAjaxCall() {
            // your ajax call from here
          }
      });
      
      如果你需要实现你所说的,下面是应该怎么做


      当对AJAX请求进行间隔延迟时,不要使用间隔,而是考虑使用超时,而只在请求完成后//P>重新调用一个新的AJAX请求。


      只需在页面加载上使用与在setInterval()中相同的函数即可

      $(文档).ready(函数(){
      //轮询公告栏消息
      var url=$.url(“/pollbuntribution.htm”);
      loadData();
      pollTimer=setInterval(loadData,pollInterval);
      函数loadData(){
      $.ajax({
      url:url,
      键入:“GET”,
      错误:函数(数据){
      //服务器可能当晚停机,或者可能出现故障
      //网络漏洞。因此,尝试轮询10次
      //如果仍然失败,取消投票。
      retryCount=retryCount+1;
      if(pollTimer!=null&&retryCount>=maxRetries){
      清除间隔(轮询计时器);
      }
      },
      成功:功能(bulletinBarMessage){
      //一旦轮询成功,请重置重试计数。
      retryCount=0;
      var respContent=“”;
      respContent+=bulletinBarMessage.messageLevel+“:”;
      respContent+=bulletinBarMessage.message;
      $(“#mt news”).html(“
        ”); } }); } });

        建议你也应该引入一个超时。

        @ SATPAL Ya,即使是OP使用的错误语法也会起作用,因为JQuEY内部使用的允诺在Ajax请求上对间隔延迟做出了使用,不要使用间隔,而是考虑使用超时,而只在请求完成后再调用新的一个。即使在这里,30秒应该足以让之前的请求完成,但谁知道会发生什么……应该是:
        pollTimer=setInterval(loadData,pollInterval)
        
        $(document).ready(function(){
        
            makeAjaxCall();
        
            setInterval(function() {
                  // Do something every 30 seconds
                  makeAjaxCall();
            }, 30000);
        
            function makeAjaxCall() {
              // your ajax call from here
            }
        });
        
        $(document).ready(function(){
        
            makeAjaxCall();
        
            function makeAjaxCall() {
              // your ajax call from here
              $.ajax({
                // option1
                // option2
                // option3
                success: function(){setTimeout(makeAjaxCall, 30)},
                error: function(){setTimeout(makeAjaxCall, 30)},
              })
            }
        });
        
        $(document).ready(function() {
        
        // Poll for bulletin bar message
        var url = $.url("/pollBulletin.htm");
        
        loadData();
        pollTimer = setInterval(loadData, pollInterval);
        
        function loadData() {
            $.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('<ul><a href="#" target="_self">' + respContent + '</a></ul>');
                }
            });
        }
        });