javascript - 如何使用用户输入创建倒数计时器
问题描述
我是网络开发的新手,我想跟踪员工的工作。因此,每当一项新任务分配给员工时,经理都必须添加他的姓名和给定的时间。添加时间后,它将从给定时间开始倒计时。它会在0上变成红色。我看过很多例子但无法实现它。我还必须在 javascript 的下拉列表中添加项目,但我不能调用它。我必须将多个员工添加到列表中。
解决方案
使用引导程序或 J 查询日期时间选择器并将该日期提供给下面的 java 脚本。
<p id="timer"></p>
$(document).ready(function () {
// Set the date we're counting down to
var countDownDate = new Date("May 30, 2018 15:16:25").getTime();
// Update the count down every 1 second
var x = setInterval(function () {
// Get todays date and time
var now = new Date().getTime();
// Find the distance between now an the count down date
var distance = countDownDate - now;
// Time calculations for days, hours, minutes and seconds
var days = Math.floor(distance / (1000 * 60 * 60 * 24));
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 *
60 * 60));
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 *
60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
// Display the result in the element with id="demo"
document.getElementById("timer").innerHTML = days + "d " + hours +
"h "
+ minutes + "m " + seconds + "s ";
// If the count down is finished, write some text
if (distance < 0) {
clearInterval(x);
document.getElementById("timer").innerHTML = "EXPIRED";
}
}, 1000);
});
推荐阅读
- javascript - async await returning [object Promise] in vue.js
- typescript - Can't figure out how conditional types work
- javascript - Javascript过滤器,如何使它们各自独立运行?
- java - 将文本从动态网页复制到字符串
- c# - .NET 5 Blazor HttpClient BaseAddressAuthorizationMessageHandler 未找到
- php - 使用 php 在 phpmyadmin 中输入适当的值(名称)而不是(id)
- sql - ORA-00907: 缺少右括号,所有括号都已关闭但仍无法正常工作
- laravel - Laravel Mix 和 Tailwind CSS
- python - 程序执行时如何打印文件路径和行号?
- python - 已修复 - 从子模块中的父文件夹导入模块 - Python 3