2014-03-26 62 views
0

我正在使用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('<ul><a href="#" target="_self">' + respContent + '</a></ul>'); 
      } 
     }); 
     // When communication with the server is lost stop the poll. 
     }, pollInterval); 
}); 

感謝

+0

@Satpal ya它應該是甚至OP所使用的錯誤語法也會工作,因爲準備好的承諾在內部使用jQuery –

+1

當談到在間隔延遲上發出ajax請求時,請不要使用間隔,請考慮使用而是僅在請求完成後才調用新的調用。即使在這裏,30secs應該足以讓以前的請求完成,但是誰知道會發生什麼...... –

回答

1

只需在頁面加載中使用與setInterval()中相同的功能即可。

$(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>'); 
     } 
    }); 
} 
}); 

由於A. Wolff建議您還應該引入超時。

+0

它應該是:'pollTimer = setInterval(loadData,pollInterval);' –

0

你應該把你的代碼中的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); 
}); 
-1

試試這個。

$(document).ready(function(){ 

    makeAjaxCall(); 

    setInterval(function() { 
      // Do something every 30 seconds 
      makeAjaxCall(); 
    }, 30000); 

    function makeAjaxCall() { 
     // your ajax call from here 
    } 
}); 

櫃面您需要實現什麼A Wollf說,繼承人應當如何做,

當談到做出間隔延遲Ajax請求,不使用的時間間隔,考慮到使用超時,而不是和召回只有一次請求已完成一個新的

$(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)}, 
     }) 
    } 
}); 
相關問題