Javascript Countdown

前端 未结 6 2104
臣服心动
臣服心动 2020-11-27 19:04

I have searched the web but all the ones readily available are where you specificy a date and it counts down to that date. What I need is something which will simply count d

6条回答
  •  無奈伤痛
    2020-11-27 19:50

    I have made a simple countdown you can use.

    It is generating the format:

    DAYS X, HOURS X, MINUTES X, SECONDS X

    The JS:

    countIt();
    
    function countIt(){
        year = 2013;
        month = 05;
        day = 28;
        hours = 12;
        minutes = 00;
        seconds = 00;
    
        setTimeout(function(){
        endDate = new Date(year, (month - 1), day, hours, minutes, seconds, 00);
        thisDate  = new Date();
        thisDate  = new Date(thisDate.getFullYear(), thisDate.getMonth(), thisDate.getDate(), thisDate.getHours(), thisDate.getMinutes(), thisDate.getSeconds(), 00, 00);
    
        var daysLeft = parseInt((endDate-thisDate)/86400000);
        var hoursLeft = parseInt((endDate-thisDate)/3600000); 
        var minutsLeft = parseInt((endDate-thisDate)/60000);
        var secondsLeft = parseInt((endDate-thisDate)/1000);
    
        seconds = minutsLeft*60;
        seconds = secondsLeft-seconds;
    
        minutes = hoursLeft*60;
        minutes = minutsLeft-minutes;
    
        hours = daysLeft*24;
        hours = (hoursLeft-hours) < 0 ? 0 : hoursLeft-hours;
    
        days = daysLeft;
    
        startCount(days, hours, minutes,seconds);
        }, 1000);
    }
    
    function startCount(days, hours, minutes, seconds){
        document.getElementById("counter").innerHTML="DAYS "+days+", HOURS "+hours+", MINUTES "+minutes+", SECONDS: "+seconds;
        countIt();
    }
    

    The HTML:

    Click here to see it live

提交回复
热议问题