2013-03-07 95 views
0

我有這個ajax呼叫是每10秒一次重複呼叫。這完美地工作但是,訪問計數器僅在頁面加載後10秒更新。在此之前,它是空的。如果我將其設置爲60秒(這更實際一些,我認爲),那麼這個區域是空白的60秒,然後纔會顯示數據。阿賈克斯呼叫延遲SetInterval

我該如何修改它以在頁面加載時立即獲取訪問值,然後每10秒不斷刷新一次?

<script type="text/javascript"> 
var Visits = 0; 

$(document).ready(function() { 
    $("#counter").flipCounter(); 
    setInterval("ajaxd()",10000); 
}); 

function ajaxd(){ // this function is called only after 10 seconds after page load 
    $.ajax({ 
    type: "POST", 
    cache:false, 
    url: "getVisits.asp?dx=1, // returns a number 
    dataType: "text", 
    data: "", 
    success: function(data){ 
     Visits= parseFloat(data).toFixed(2); 
    } 
}); 
/// update value in <div> 

$("#counter").flipCounter(
"startAnimation", // scroll counter from the current number to the specified number 
{ 
start_number: 0, // the number we want to scroll from 
end_number: Visits, // the number we want the counter to scroll to 
numIntegralDigits: Visits.length-3, // number of places left of the decimal point to maintain 
numFractionalDigits:2, // number of places right of the decimal point to maintain 
easing: jQuery.easing.easeOutCubic, // this easing function to apply to the scroll. 
duration: 3000 // number of ms animation should take to complete 
}); 
} 

最後,這部分在計數器顯示:

<div id="counter" class="bb_box"> 
<input type="hidden" name="counter-value" value="00.00"/> 
</div> 
+1

從來沒有使用字符串PARAM中的setInterval,因爲它實際上eval的特殊形式。 – shabunc 2013-03-07 08:44:58

+0

我願意接受任何其他解決方案,可以做到這一點 – JamesT 2013-03-07 09:18:38

回答

0

變化

$(document).ready(function() { 
    $("#counter").flipCounter(); 
    setInterval("ajaxd()",10000); 
}); 

$(document).ready(function() { 
    $("#counter").flipCounter(); 
    setInterval(ajaxd,10000); 
    ajaxd(); // add this to call ajax once immediately 
}); 

並將此代碼塊移到腳本塊的末尾,以避免調用未定義的函數。

UPDATE 你可以嘗試這樣的:

var Visits = 0; 

function ajaxd(){ 
    $.ajax({ 
    type: "POST", 
    cache:false, 
    url: "getVisits.asp?dx=1, // returns a number 
    dataType: "text", 
    data: "", 
    success: function(data){ 
     Visits= parseFloat(data).toFixed(2); 
     $("#counter").flipCounter("setNumber", Visits); 
    } 
}); 

$(document).ready(function() { 
    $("#counter").flipCounter(); 
    setInterval(ajaxd, 10000); 
    ajaxd(); 
}); 
+0

嗨戈蘭,這沒有奏效。仍然不會做任何事情10秒。 – JamesT 2013-03-07 09:12:59

+0

控制檯中是否有錯誤?你可以嘗試在頁面加載時在js控制檯中鍵入'ajaxd()'嗎?你可以添加一個console.log(「ajaxd called」)行到你的ajaxd函數來檢查它是否被調用? – 2013-03-07 09:20:09

+0

類型ajaxd()在控制檯中,得到了如下錯誤 ajaxd() 不確定 – JamesT 2013-03-07 10:46:49

0

試試這個(見ajaxd()是函數後)

setInterval(function() { 
ajaxd(); 

}, 10000); 

function ajaxd(){ // this function is called only after 10 seconds after page load 
    $.ajax({ 
    type: "POST", 
    cache:false, 
    url: "getVisits.asp?dx=1, // returns a number 
    dataType: "text", 
    data: "", 
    success: function(data){ 
     Visits= parseFloat(data).toFixed(2); 
    } 

ajaxd(); 
+0

好的,我會試試這個,然後回來。謝謝 – JamesT 2013-03-07 17:47:40

+0

我使用一些類似的代碼來填充圖表並在1分鐘內刷新,圖表在頁面加載時從服務器加載並在每分鐘刷新一次。 – Kris 2013-03-08 02:50:27