如何创建JQuery时钟/计时器

Gan*_*kar 50 javascript jquery timer clock

我有一个简单的测验应用程序,我希望在页面顶部显示一个漂亮的计时器/时钟,向用户显示他们已经用了多长时间.(一旦我有一个计时器工作,如果我能以某种方式向他们展示总测验时间的计时器,并且也是第二个对于这个问题时,这将是更酷,但我应该能够找出如何做自己.

我的问题是:

使用JQuery显示简单的计时器/时钟有什么好的,简单的方法?(直接JS也行不通)我知道如何检查时间,但是如何增加秒数呢?

我自己的搜索继续引导我到JQuery插件(我想自己动手)和"事件计时器",这不是我正在寻找...

SLa*_*aks 98

您正在寻找该setInterval函数,该函数每隔x毫秒运行一次函数.

例如:

var start = new Date;

setInterval(function() {
    $('.Timer').text((new Date - start) / 1000 + " Seconds");
}, 1000);
Run Code Online (Sandbox Code Playgroud)

  • 这段代码看起来不错.$('.Timer').text(Math.round((new Date - start)/ 1000,0)+"Seconds"); (5认同)
  • 给我1.001秒,2.001,3.001等.从10秒开始,小数点消失了.猜猜我们需要一个圆形()吗? (3认同)

Gan*_*kar 39

SLaks建议的setInterval正是我制作计时器所需要的.(谢了哥们!)

使用setInterval和这篇伟大的博客文章,我最终创建了以下函数来在我的"box_header"div中显示一个计时器.我希望这可以帮助其他有类似要求的人!

 function get_elapsed_time_string(total_seconds) {
  function pretty_time_string(num) {
    return ( num < 10 ? "0" : "" ) + num;
  }

  var hours = Math.floor(total_seconds / 3600);
  total_seconds = total_seconds % 3600;

  var minutes = Math.floor(total_seconds / 60);
  total_seconds = total_seconds % 60;

  var seconds = Math.floor(total_seconds);

  // Pad the minutes and seconds with leading zeros, if required
  hours = pretty_time_string(hours);
  minutes = pretty_time_string(minutes);
  seconds = pretty_time_string(seconds);

  // Compose the string for display
  var currentTimeString = hours + ":" + minutes + ":" + seconds;

  return currentTimeString;
}

var elapsed_seconds = 0;
setInterval(function() {
  elapsed_seconds = elapsed_seconds + 1;
  $('#box_header').text(get_elapsed_time_string(elapsed_seconds));
}, 1000);
Run Code Online (Sandbox Code Playgroud)

  • 嘿,不要相信函数将每1000毫秒运行一次,你就会失去同步.您需要存储开始日期并每次计算差异,如此问题的已接受答案中所示 (11认同)

Uda*_*ale 14

################## JQuery (use API) #################   
 $(document).ready(function(){
         function getdate(){
                var today = new Date();
            var h = today.getHours();
            var m = today.getMinutes();
            var s = today.getSeconds();
             if(s<10){
                 s = "0"+s;
             }
             if (m < 10) {
                m = "0" + m;
            }
            $("h1").text(h+" : "+m+" : "+s);
             setTimeout(function(){getdate()}, 500);
            }

        $("button").click(getdate);
    });

################## HTML ###################
<button>start clock</button>
<h1></h1>
Run Code Online (Sandbox Code Playgroud)

  • 优秀!如果使用jQuery(我这样做),显示时钟的简单,直接的方式.我为有兴趣看到这个的人创造了一个jsfiddle.在这里查看... http://jsfiddle.net/smallworld/ng22Y/1/ (4认同)

小智 9

这两个世界的优点怎么样?我将答案与OP的格式结合起来.

function pretty_time_string(num) {
    return ( num < 10 ? "0" : "" ) + num;
  }

var start = new Date;    

setInterval(function() {
  var total_seconds = (new Date - start) / 1000;   

  var hours = Math.floor(total_seconds / 3600);
  total_seconds = total_seconds % 3600;

  var minutes = Math.floor(total_seconds / 60);
  total_seconds = total_seconds % 60;

  var seconds = Math.floor(total_seconds);

  hours = pretty_time_string(hours);
  minutes = pretty_time_string(minutes);
  seconds = pretty_time_string(seconds);

  var currentTimeString = hours + ":" + minutes + ":" + seconds;

  $('.timer').text(currentTimeString);
}, 1000);
Run Code Online (Sandbox Code Playgroud)


Kur*_*den 7

24 小时制:

setInterval(function(){

        var currentTime = new Date();
        var hours = currentTime.getHours();
        var minutes = currentTime.getMinutes();
        var seconds = currentTime.getSeconds();

        // Add leading zeros
        minutes = (minutes < 10 ? "0" : "") + minutes;
        seconds = (seconds < 10 ? "0" : "") + seconds;
        hours = (hours < 10 ? "0" : "") + hours;

        // Compose the string for display
        var currentTimeString = hours + ":" + minutes + ":" + seconds;
        $(".clock").html(currentTimeString);

},1000);
Run Code Online (Sandbox Code Playgroud)

setInterval(function(){

        var currentTime = new Date();
        var hours = currentTime.getHours();
        var minutes = currentTime.getMinutes();
        var seconds = currentTime.getSeconds();

        // Add leading zeros
        minutes = (minutes < 10 ? "0" : "") + minutes;
        seconds = (seconds < 10 ? "0" : "") + seconds;
        hours = (hours < 10 ? "0" : "") + hours;

        // Compose the string for display
        var currentTimeString = hours + ":" + minutes + ":" + seconds;
        $(".clock").html(currentTimeString);

},1000);
Run Code Online (Sandbox Code Playgroud)
// 24 hour clock  
setInterval(function() {

  var currentTime = new Date();
  var hours = currentTime.getHours();
  var minutes = currentTime.getMinutes();
  var seconds = currentTime.getSeconds();

  // Add leading zeros
  hours = (hours < 10 ? "0" : "") + hours;
  minutes = (minutes < 10 ? "0" : "") + minutes;
  seconds = (seconds < 10 ? "0" : "") + seconds;

  // Compose the string for display
  var currentTimeString = hours + ":" + minutes + ":" + seconds;
  $(".clock").html(currentTimeString);

}, 1000);
Run Code Online (Sandbox Code Playgroud)