javascript countdown clock

前端 未结 4 653
执念已碎
执念已碎 2021-01-03 19:05
 

        
4条回答
  •  醉话见心
    2021-01-03 19:45

    I've cleaned up the above code and made it format like your example. It also removed the global variables and allows you to create multiple timers.

    There's a live link here http://jsfiddle.net/Apnu2/6/

    countdown('countdown', 1, 5);
    function countdown(element, minutes, seconds) {
        // set time for the particular countdown
        var time = minutes*60 + seconds;
        var interval = setInterval(function() {
            var el = document.getElementById(element);
            // if the time is 0 then end the counter
            if(time == 0) {
                el.innerHTML = "countdown's over!";    
                clearInterval(interval);
                return;
            }
            var minutes = Math.floor( time / 60 );
            if (minutes < 10) minutes = "0" + minutes;
            var seconds = time % 60;
            if (seconds < 10) seconds = "0" + seconds; 
            var text = minutes + ':' + seconds;
            el.innerHTML = text;
            time--;
        }, 1000);
    }
    

提交回复
热议问题