Trying to make a reusable function and display the result in a div
        Posted  
        
            by 
                user3709033
            
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by user3709033
        
        
        
        Published on 2014-06-06T15:13:44Z
        Indexed on 
            2014/06/06
            15:25 UTC
        
        
        Read the original article
        Hit count: 221
        
I have this function that I am trying to work with. All I am trying to do is to get the values of days, hours, min and sec into the .overTime div. I want to use the same function over and over again because i have more divs in which I want to display the same values but in a diff manner.
You guys are awesome Thank You.
    NowTime = new Date(); //Time Now
StartTime = new Date($('#StartTime').val());
StopTime = new Date($('#StopTime').val());
function fixIntegers(integer){
    if (integer < 0)
        integer = 0;
    if (integer < 10)
        return '0' + integer;
    return '' + integer;
}  
    function Test( difference )
    {
        var toReturn = { days: 0, hours: 0, minutes: 0, seconds: 0 };
        toReturn.seconds = fixIntegers(difference % 60);
        difference = Math.floor(difference / 60);    
        toReturn.minutes = fixIntegers(difference % 60);
        difference = Math.floor(difference / 60);
        toReturn.hours = fixIntegers(difference % 24);
        difference = Math.floor(difference / 24);
        toReturn.days = fixIntegers(difference);
        return toReturn;
    }
    function run()
    {
        var output = Test( Math.floor( ( NowTime - StopTime ) / 1000 ) );
        $('.OverTime').html( output.days + ':' + output.hours + ':' + output.minutes + ':' + seconds);
    }
setInterval(run, 1000)
FIDDLE: http://jsfiddle.net/8943U/47/
© Stack Overflow or respective owner