- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要在一页上创建多个倒计时时钟。目前我只有一个,但我想让脚本变得漂亮和紧凑,所以我想对日期使用相同的计算,但只是提取不同的值。
英联邦运动会开始倒计时。我需要另一个倒计时闭幕式仪式。
脚本:
// set the date we're counting down to
var target_date_opening = new Date("Jul 23, 2014").getTime();
var target_date_closing = new Date("Aug 23, 2014").getTime();
// variables for time units
var days, hours, minutes, seconds;
// get tag element
var countdownOpening = document.getElementById("countdownOpening");
var countdownClosing = document.getElementById("countdownClosing");
// update the tag with id "countdownOpening" every 1 second
setInterval(function () {
// find the amount of "seconds" between now and target
var current_date = new Date().getTime();
var seconds_left = (target_date_opening - current_date) / 1000;
// do some time calculations
days = parseInt(seconds_left / 86400);
seconds_left = seconds_left % 86400;
hours = parseInt(seconds_left / 3600);
seconds_left = seconds_left % 3600;
minutes = parseInt(seconds_left / 60);
seconds = parseInt(seconds_left % 60);
// format countdown string + set tag value
countdownOpening.innerHTML = days + " days " + hours + " hours "
+ minutes + " minutes " + seconds + " seconds";
}, 1000);
您可以看到我创建了新变量来区分目标日期 (target_date_ending
)
我有 innerHTML
的 ID 作为 countdownClosing。
我的问题是让 setInterval
函数计算 target_date_ending
并输出 countdownClosing
,而无需再次复制脚本。
我不确定是否需要单独的变量来存储分钟
、天
、小时
和秒
变量。
最佳答案
尝试
// set the date we're counting down to
var target_date_opening = new Date("Jul 23, 2014").getTime();
var target_date_closing = new Date("Aug 23, 2014").getTime();
// variables for time units
var days, hours, minutes, seconds;
// get tag element
var countdownOpening = document.getElementById("countdownOpening");
var countdownClosing = document.getElementById("countdownClosing");
// update the tag with id "countdownOpening" every 1 second
setInterval(function () {
print(countdownOpening, target_date_opening)
print(countdownClosing, target_date_closing)
}, 1000);
function print(el, time) {
// find the amount of "seconds" between now and target
var current_date = new Date().getTime();
var seconds_left = (time - current_date) / 1000;
// do some time calculations
var days = parseInt(seconds_left / 86400);
seconds_left = seconds_left % 86400;
var hours = parseInt(seconds_left / 3600);
seconds_left = seconds_left % 3600;
var minutes = parseInt(seconds_left / 60);
var seconds = parseInt(seconds_left % 60);
// format countdown string + set tag value
el.innerHTML = days + " days " + hours + " hours " + minutes + " minutes " + seconds + " seconds";
}
演示:Fiddle
关于javascript - 多个 "clocks"的倒计时JavaScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21379425/
这个问题在这里已经有了答案: What specifically are wall-clock-time, user-cpu-time, and system-cpu-time in UNIX? (4
我只是尝试使用 jasmine 在 javascript/coffeescript 中进行单元测试,非常好。 我一直在尝试使用 jasmine.Clock.Mock() 来提前并触发 setTimeo
现在使用 emacs org 模式大约一个月来跟踪我所有的项目和任务。 我全天为所有事件计时,而不仅仅是与工作相关的事件。 我的问题是——我经常忘记打卡参加一项新事件(比如吃午饭)。当我返回并打卡回到
我在看 Clock来自java的类,以便我可以将其作为依赖项注入(inject)以获得更好的可测试性。但是我不明白这两种方法的区别systemUTC和 systemDefaultZone . Inst
当我设置特定日期时,我正在努力测试我的端点。 我不想使用 PowerMock 来模拟静态方法,而是决定更改我的服务的实现并使用 LocalDate.now(Clock clock) 实现来更容易地测试
我有一个员工“上类打卡”表,具有开始时间和结束时间属性。每位员工上下类打卡,表格类似这样: Employee_no Start time End time 16
Linux perf工具(前段时间命名为 perf_events)有几个内置的通用软件事件。其中最基本的两个是:task-clock和 cpu_clock (内部称为 PERF_COUNT_SW_CP
我的方法之一中有以下代码 ZonedDateTime current = Instant.now().atZone(ZoneId.of(AMERICA_NEW_YORK)); 我想在 JUnit 测试
我正在使用 sf::Clock 来控制循环。 我想知道是否允许我使用超过 1 个 sf::Clock,如果允许,是否所有“时钟”都将正常运行并按预期工作在所有操作系统上。 例如: sf::Clock
假设您有一个 (1) Intel/AMD x86-64 位 2 GHz 8 核处理器。 8 个内核中的每一个是否都以完整的 2 GHz 运行,或者每个内核是否以完整的 2 GHz 时钟的一部分运行(例
对于 java 8 下面的代码 1. System.out.println(LocalDateTime.now(Clock.systemDefaultZone())); 2. System.o
我有以下使用Chatterbot第三方库的代码:。当我尝试使用代码时,从Visual Studio收到如下错误:。我安装了以下程序包:。我尝试了使用Python3.9和3.11以及Chatterbot
我有以下使用Chatterbot第三方库的代码:。当我尝试使用代码时,从Visual Studio收到如下错误:。我安装了以下程序包:。我尝试了使用Python3.9和3.11以及Chatterbot
我有以下使用Chatterbot第三方库的代码:。当我尝试使用代码时,从Visual Studio收到如下错误:。我安装了以下程序包:。我尝试了使用Python3.9和3.11以及Chatterbot
我有一个相对简单的要求:我希望我在 KVM 下创建的 CentOS 客户机上的时钟从 VM 的第一次启动就与它们的 CentOS 主机同步。 在它们启动并运行后将它们与 NTP 同步很容易。但是,如果
谁能告诉我如何找到STM32F429的APB1时钟频率?以及如何计算STM32F429的波特率。 P.S...请不要告诉我引用引用手册,因为这些工作已经完成,我想告诉我 APB1 时钟频率的确切值 谢
我在 LAN 上有 5 台服务器,没有 Internet 连接。我需要他们保持时钟同步。 我可以将它们配置为 NTP 对等体,并为其中一个的本地时钟设置较高的层数。这样,其他四个就会与该时钟同步。 我
让我们假设我们有许多系统连接在本地网络中并且没有连接到互联网。确保每个时钟同步的最佳方法是什么?它们不一定要与 UTC 时间同步,但在它们之间保持同步就足够了。 我曾想过通过在其中一个系统中设置 NT
我对 clock() 有疑问在运行 32 位版本的 Debian 6.0.3 的机器上。它似乎总是返回 0。使用 this example program 时,我得到以下输出: Calculating
我正在尝试编写一个程序来比较快速排序和插入排序函数所花费的时间,具体取决于数组中元素的数量。这是我想出的代码: #include #include #include //prototypes o
我是一名优秀的程序员,十分优秀!