Search code examples
primefacestimerstartupprimefaces-extensions

How to set a start value for a primefaces extension timer


I use a primefaces extension timer with the following code :

<pe:timer 
    style="color:darkgrey;"
    timeout="1000"
    forward="false" 
    format="HH:mm:ss"/>

But I have a start value. The previous code start from the value 00:00:00, but I have a start time from a Java bean. I have a Date object, or date as long type (from 1970) which is the value from I want to start.

For example I got 1548434800083 or 17:47 25/01/2019 and I want to display the time between this date and now. So how I can set my start value with this date ? I got milliseconds but I can get seconds instead of.


Solution

  • The solution is the following, I have a Java bean, that contain a date in millisecond. I get it with the function getTime() from the Java class Date.

    I display my counter, chronometer with the primefaces code :

    <pe:timer 
       style="color:grey;"
       forward="true" 
       formatFunction="return displayElapsedTime('#{synopticBean.longEnteredTime}');"/>
    

    That code call a javascript function, that return a format counter/chronometer :

    function displayElapsedTime(longEnteredTime){
            var now = new Date();
            var elapsedTime = now.getTime() - longEnteredTime;
            var numHours = Math.floor(elapsedTime / 3600000);
    
            var minutesAndSecondMS = elapsedTime - numHours;
            var minutes = Math.floor((minutesAndSecondMS % (1000 * 60 * 60)) / (1000 * 60));
            var seconds = Math.floor((minutesAndSecondMS % (1000 * 60)) / 1000);
    
            var innerHtmlText = (('0' + (numHours)).slice(-2) + ":" +('0' + (minutes)).slice(-2) + ":" + ('0' + (seconds)).slice(-2));
    
            return innerHtmlText;
        }
    

    It display a duration, the number of hours. I only count with hours, no day.