我有这个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>
从来没有使用字符串PARAM中的setInterval,因为它实际上eval的特殊形式。 – shabunc 2013-03-07 08:44:58
我愿意接受任何其他解决方案,可以做到这一点 – JamesT 2013-03-07 09:18:38