1

I'm developing an application that shows a clock to the user in order to control the execution time for tasks and log. The clock should have the server hour so it can't be modified by the user.

The problem is that when the time runs, the actualizarTiempo function each time executes in larger intervals, making the clock look like the image below.

What can I do to guarantee that the actualizarTiempo function executes each second in an indefinite manner?

My Java Code:

package bean.controlador;

import java.io.IOException;
import static java.lang.Thread.sleep;
import java.util.Calendar;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

@ManagedBean
@SessionScoped
public class Calendario {
    Calendar calendario;
    String tiempo;
    public Calendario() {
    }
    public void load() throws IOException, InterruptedException{
        actualizarTiempo();
        FacesContext.getCurrentInstance().getExternalContext().redirect("registroActividades.xhtml");
    }

    public void actualizarTiempo() throws InterruptedException{
        calendario = Calendar.getInstance();
        tiempo = Integer.toString(calendario.get(Calendar.HOUR_OF_DAY)) + ":" +Integer.toString(calendario.get(Calendar.MINUTE)) + ":" + Integer.toString(calendario.get(Calendar.SECOND));
    }

    public Calendar getCalendario() {
        return calendario;
    }

    public void setCalendario(Calendar calendario) {
        this.calendario = calendario;
    }

    public String getTiempo() {
        return tiempo;
    }

    public void setTiempo(String tiempo) {
        this.tiempo = tiempo;
    }

}

My XHTML code:

<h:panelGroup id = "panelNuevo">
    <h:outputText value = "#{calendario.tiempo}"/>
</h:panelGroup>
<h:form>
    <p:poll interval="1" listener="#{calendario.actualizarTiempo}" update=":panelNuevo" />
</h:form>

Thank you!

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Juan C
  • 89
  • 7

0 Answers0