2015-11-03 88 views
-1

设置的时间间隔显示页面上的倒数计时器。下面的代码很不幸,它不显示时间。我不是在寻找精心设计的冗长代码,而是能够完成工作的几行代码。是否有一个jQuery或JavaScript函数,让我有一个倒数计时器

<script> 
    function myFunction() { 
    setInterval(function(){ alert("Hello"); }, 3000); 
} 
</script> 

编辑:我不需要提醒。计时器需要显示在页面上。

+0

谢谢大家。我从每个答案中学到了一些新东西。抬头。 – Ann

回答

2

使用countdownTimer jquery库。我指导你如何做到这一点。首先下载jquery.countdownTimer.js和jquery.countdownTimer.css文件。

html应该如下。

<!-- link java script and css --> 
<script type="text/javascript" src="jquery-2.0.3.js"></script> 
<script type="text/javascript" src="jquery.countdownTimer.js"></script> 
<link rel="stylesheet" type="text/css" href="jquery.countdownTimer.css" /> 

<div id="countdowntimer"><span id="future_date"><span></div> 

Javascript应该像下面这样。

<script type="text/javascript"> 
$(function(){ 
    $("#future_date").countdowntimer({ 
     dateAndTime : "2015-12-25 00:00:00", 
    size : "lg", 
     regexpMatchFormat: "([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})", 
      regexpReplaceWith: "$1<sup>days</sup>/$2<sup>hours</sup>/$3<sup>minutes</sup>/$4<sup>seconds</sup>" 
    }); 
}); 
</script> 

有以下url的演示,你也可以从demo下载所有需要的演示文件。 http://harshen.github.io/jquery-countdownTimer/

+0

最佳答案。我只想用一分钟。代码分钟的变化很小。 '分钟:2尺寸:“lg”'这个工作后我删除了这一行'尺寸:“LG”'。 – Ann

1

http://momentjs.com/

你应该看一看这一点。这是非常有据可查的,你有很多方法来格式化你的柜台

1

getTime()方法返回自January 1, 1970午夜以来的毫秒数。

var startTime = new Date().getTime(); 

// your code 

var endTime = new Date().getTime(); 
var timeTaken = endTime - startTime; 
alert('Execution time: ' + timeTaken); 

您也可以使用console.time

console.time('Function1'); 

Function1(); 

console.timeEnd('Function1'); 
相关问题