有两个问题:
我如何在这里实现一些调用#refresh
以允许以文本形式计数的计时器的javascript,假设从0秒开始,运行最多3分钟然后刷新并从零开始 - 刷新id所在的特定div?
$(document).ready(function () {
var interval = 180000; // set for 3 minutes
var refresh = function() {
$.ajax({
url: "servers.php",
cache: false,
success: function(html) {
$('#refresh').html(html);
setTimeout(function() {
refresh();
}, interval);
}
});
};
refresh();
});
<div id="refresh"><p>Last Scanned: ???</p></div>
另外,我对AJAX还是比较陌生的,所以如果上面的代码不正常或者不正确,请告诉我以及我出错的地方......
答案 0 :(得分:2)
好的,这是整个计数器的格式,没有AJAX:
$( document ).ready( function(){
setTime(180001); // set for 3 minute interval scans
});
function setTime(interval){
setTimeout(startCounter(interval, 0),interval);
}
function startCounter(target, current) {
var mins, secs;
if (current >=60) {
mins = Math.floor(current/60);
secs = current - (mins*60);
$('#refresh p').html('Last Scanned: '+ mins + 'm ' + secs + 's ago');
}else $('#refresh p').html('Last Scanned: '+ current + 's ago');
if (current >= target/1000) {
setTime(180001);
return;
}
setTimeout(function(){startCounter(target, current+1);}, 1000);
}
jsFiddle示例:http://jsfiddle.net/vcLhy/7/