Search code examples
primefacestimermomentjsprimefaces-extensions

Primefaces extensions timer


I'm trying to make primefaces extentions countdown timer in format something like this:

Time remaning: 3 days, 01 : 15 : 22

So i added pe:timer component and set format like this:

<pe:timer id="time_remaning" timeout="800" format="DDD [days,] HH:mm:ss" immediate="true"/>

But its not working as expected. I'm getting: 1 days, 00 : 13 : 20

but "days" part of timeout should be 0 and not 1. Do i have error in my formater? I got formater parameters from here as decribed in documentation.

Or do I need to use custom formatFunction for this example or is there a way to do this just with formater?


Solution

  • I could not make it to work using formater so i used functionFormater to get the job done.

    Here is my .xhtml code:

    <pe:timer id="time_remaning" timeout="#{myBean.getTimeForTimeoutInSeconds()}" 
        formatFunction="return formatMe(value);" />
    

    And the .js format function:

    window.formatMe = function (value) {
        var numDays = Math.floor(value / 86400);
        var numHours = Math.floor((value % 86400) / 3600);
        var numMinutes = Math.floor(((value % 86400) % 3600) / 60);
        var numSeconds = ((value % 86400) % 3600) % 60;
        return numDays + " dni, " + pad(numHours, 2) + " : "
            + pad(numMinutes, 2) + " : " + pad(numSeconds, 2) + " ";
    }
    
    function pad(num, size) {
        var s = num+"";
        while (s.length < size) s = "0" + s;
        return s;
    }