Time and Timers

Time-Out

// Time-Outs
// Calls a function or executes a code snippet after a specified delay.
setTimeout( function(){
    // Do something here.
}, 5000);

// Or...
function message(){ alert( 'Hi' ); }
setTimeout( message, 1000 );

Timed Intervals

// Timed Intervals
// Calls a function or executes a code snippet repeatedly,
// with a fixed time delay between each call to that function.
// Returns an intervalID.
var intervalTimer = window.setInterval( function(){
    // Do something here.
}, 6000);

// Or...
function message(){ alert( 'Hi' ); }
var intervalTimer = window.setInterval( message, 6000);

// Stop the interval.
clearInterval( intervalTimer );

Convert Seconds to Time


// Convert Seconds to Time
function convertSecondsToTime( secondsIn ){
        
        var seconds = Math.round( secondsIn );
        var output = '';
        var minutes = 0;
        var hours = 0;
        
        minutes = Math.floor(seconds/60);
        seconds = seconds-(minutes*60);
        
        if(seconds.toString().length == 1){
            seconds = "0" + seconds;
        }
        
        hours = Math.floor(minutes/60);
        minutes = minutes-(hours*60);
        
        if(minutes.toString().length == 1){
            minutes = "0" + minutes;
        }
        
        if(hours > 0){
            output += hours + ':';
        }
        
        output += minutes + ':' + seconds;
        return output;
        
    }