首页 > 解决方案 > 如何使用用户输入创建倒数计时器

问题描述

我是网络开发的新手,我想跟踪员工的工作。因此,每当一项新任务分配给员工时,经理都必须添加他的姓名和给定的时间。添加时间后,它将从给定时间开始倒计时。它会在0上变成红色。我看过很多例子但无法实现它。我还必须在 javascript 的下拉列表中添加项目,但我不能调用它。我必须将多个员工添加到列表中。

标签: javascripthtml

解决方案


使用引导程序或 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);
});

推荐阅读