Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript timer countdown with seconds 59

function startTimer(duration, display) {
    var start = Date.now(),
        diff,
        minutes,
        seconds;
    function timer() {
        // get the number of seconds that have elapsed since 
        // startTimer() was called
        diff = duration - (((Date.now() - start) / 1000) | 0);

        // does the same job as parseInt truncates the float
        minutes = (diff / 60) | 0;
        seconds = (diff % 60) | 0;

        minutes = minutes < 10 ? "0" + minutes : minutes;
        seconds = seconds < 10 ? "0" + seconds : seconds;

        display.textContent = minutes + ":" + seconds; 

        if (diff <= 0) {
            // add one second so that the count down starts at the full duration
            // example 05:00 not 04:59
            start = Date.now() + 1000;
        }
    };
    // we don't want to wait a full second before the timer starts
    timer();
    setInterval(timer, 1000);
}

window.onload = function () {
    var fiveMinutes = 60 * 5,
        display = document.querySelector('#time');
    startTimer(fiveMinutes, display);
};
Comment

PREVIOUS NEXT
Code Example
Javascript :: adonisjs char 
Javascript :: upload text file react js functional component 
Javascript :: microbit hello world 
Javascript :: suitescript dialog box 
Javascript :: .net core json store data type in model oracle 
Javascript :: javascript neue zeilekill 
Javascript :: react native android build location 
Javascript :: javascript handle updation of copy object 
Javascript :: JavaScript Rules for Naming JavaScript Variables 
Javascript :: eager loading 
Javascript :: javascript Update Values of Properties 
Javascript :: javascript Passing undefined Value 
Javascript :: javascript get() handler 
Javascript :: JavaScript Generator Throw Method 
Javascript :: js console.log callstack 
Javascript :: simple counter with react hook 
Javascript :: javascript get days difference between two dates 
Javascript :: mongoose schema index of multiple columns 
Javascript :: change rotation phaser 
Javascript :: phaser rotate around x y point 
Javascript :: phaser show animation play through js 
Javascript :: toast waning 
Javascript :: hook use effect with class 
Javascript :: show a variable value in an html webpage using dom javascript 
Javascript :: javascript detect if browser is not google chrome 
Javascript :: javascript static methods 
Javascript :: javascript event.target 
Javascript :: reactnative timepicker 
Javascript :: javascript unicode character 
Javascript :: find function in javascript 
ADD CONTENT
Topic
Content
Source link
Name
5+9 =