如何修改默认倒数计时器?

时间:2019-02-13 05:14:14

标签: javascript

基于此项目(https://codepen.io/mattlitzinger/pen/ysowF),该项目非常出色,但是我想修改此代码(目标日期和特定日期)。此js代码

    var target_date = new Date().getTime() + (1000*3600*48); // set the countdown date
    var days, hours, minutes, seconds; // variables for time units

    var countdown = document.getElementById("tiles"); // get tag element

    getCountdown();

    setInterval(function () { getCountdown(); }, 1000);

    function getCountdown(){

// find the amount of "seconds" between now and target
var current_date = new Date().getTime();
var seconds_left = (target_date - current_date) / 1000;

days = pad( parseInt(seconds_left / 86400) );
seconds_left = seconds_left % 86400;

hours = pad( parseInt(seconds_left / 3600) );
seconds_left = seconds_left % 3600;

minutes = pad( parseInt(seconds_left / 60) );
seconds = pad( parseInt( seconds_left % 60 ) );

// format countdown string + set tag value
countdown.innerHTML = "<span>" + days + "</span><span>" + hours + "</span><span>" + minutes + "</span><span>" + seconds + "</span>"; 
     }

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

然后我要将目标日期设置为2019年3月5日23:59:59。我知道,我必须修改此代码,但是我对这个js代码不熟悉。 有人可以帮忙吗?

2 个答案:

答案 0 :(得分:0)

是的,是的,您只需要将目标日期值更改为

var target_date = new Date(2019,2,5,23,59,59,0).getTime();

检查此:https://codepen.io/anon/pen/qgyELL

您总是可以参考文档以获取更多帮助,日期格式为new Date(year, month, day, hours, minutes, seconds, milliseconds)

文档链接:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

答案 1 :(得分:0)

您还可以通过以下方式更改目标值:

node-sass-chokidar --include-path ./src --include-path ./node_modules src/ -o src/ && react-scripts build

参考:https://www.w3schools.com/howto/howto_js_countdown.asp