0

有点两部分的问题:

我将如何在这里实现一些javascript来调用#refresh以允许文本形式的计数计时器,假设从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 还很陌生,所以如果上面的代码不正常或不正确,请告诉我以及我哪里出错了......

4

1 回答 1

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/

于 2013-10-18T02:17:15.983 回答