javascript - 使用 future 日期的小时、分钟和秒创建计时器倒计时

标签 javascript

<分区>

我正在使用我在 Internet 上找到的一些代码来创建从某个日期开始的倒计时。我正在尝试编辑代码,以便它只为我提供从我指定的 future 日期开始的一小时、一分钟和一秒的倒计时。我不能只有从指定时间倒计时的代码,我需要它倒计时到将来的指定日期。这很重要,这样如果刷新浏览器,倒计时不会重新开始,而是从停止的地方继续。我将使用 cookie,以便浏览器记住首次运行时指定的 future 日期。

这是 HTML:

<form name="count">
<input type="text" size="69" name="count2">
</form>

这是javascript:

window.onload = function()  {
//change the text below to reflect your own,
var montharray=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")

function countdown(yr,m,d){
var theyear=yr; var themonth=m; var theday=d
var today=new Date()
var todayy=today.getYear()
if (todayy < 1000)
todayy+=1900;
var todaym=today.getMonth()
var todayd=today.getDate()
var todayh=today.getHours()
var todaymin=today.getMinutes()
var todaysec=today.getSeconds()
var todaystring=montharray[todaym]+" "+todayd+", "+todayy+" "+todayh+":"+todaymin+":"+todaysec

futurestring=montharray[m-1]+" "+d+", "+yr

var dd=Date.parse(futurestring)-Date.parse(todaystring)
var dday=Math.floor(dd/(60*60*1000*24)*1)
var dhour=Math.floor((dd%(60*60*1000*24))/(60*60*1000)*1)
var dmin=Math.floor(((dd%(60*60*1000*24))%(60*60*1000))/(60*1000)*1)
var dsec=Math.floor((((dd%(60*60*1000*24))%(60*60*1000))%(60*1000))/1000*1)
if(dday==0&&dhour==0&&dmin==0&&dsec==1){
document.forms.count.count2.value=current
return
}
else
document.forms.count.count2.value= dhour+":"+dmin+":"+dsec;
setTimeout(function() {countdown(theyear,themonth,theday)},1000)
}
//enter the count down date using the format year/month/day
countdown(2012,12,25)
}

我确信上面有多余的代码,因为我只需要一个小时、分钟和秒,我想将其传递给 countdown() 函数。年、月和日并不重要,但正如我所说,这是我正在尝试编辑的代码,是我在互联网上找到的。任何帮助将不胜感激。谢谢!

最佳答案

您可以为目标时间创建一个日期对象,并获取与当前日期对象的差异。请注意,这取决于客户端是否正确设置了时钟。

function timeDiff(target) {

  function z(n) {return (n<10? '0' : '') + n;}

  var timeDiff = target - (new Date());
  var hours    = timeDiff / 3.6e6 | 0;
  var minutes  = timeDiff % 3.6e6 / 6e4 | 0;
  var seconds  = timeDiff % 6e4 / 1e3 | 0;

  return z(hours) + ':' + z(minutes) + ':' + z(seconds);
}

alert(timeDiff(new Date(2012,9,23,17,50,0)));

每秒运行一次,下一整秒后几毫秒。我会把它留给你。

编辑

搞什么鬼,这里有一个计时器可以调用它。只需要文档中一个 id 为 "timer"的元素:

function doCountDown(target) {
  document.getElementById('timer').innerHTML = timeDiff(target);
  var lag = 1020 - (new Date() % 100);
  setTimeout(function(){doCountDown(target);}, lag);
}

window.onload = function() {
  doCountDown(new Date(2012,9,23,17,50,0));
}

关于javascript - 使用 future 日期的小时、分钟和秒创建计时器倒计时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13023756/

相关文章:

javascript - 为什么当我关闭 SharePoint 中的模式对话框时无法重定向到另一个 URL?

javascript - 在 Bootstrap 中选择项目时如何更改下拉菜单的主要显示

javascript - 调用 fetch() 时,主干集合给出 "Ajax of undefined"错误

javascript - dom 搜索 id 返回错误 - 如何处理?

javascript - 用其内容替换组件 - angular 2

javascript - 格式化字符串和日期时间?

javascript - 创建一个纯基于 javascript 的视频播放器

javascript - 根据选项选择更改 Bootstrap slider 中的值

javascript - JQuery 模板 : How to preserve backslashes in content?

javascript - 此代码的输出必须是 john james 和 rose。但是 j o h 出现在输出中。为什么?