Fire ajax event every 10 seconds after page is loaded

ajax, jsf

Solution

This process is called "polling".

This feature is not provided by the standard JSF `<f:ajax>`. You'd need to look for a 3rd party JSF component library. Based on your question history, you're using PrimeFaces, or at least already familiar with it. In that case, you can just use its `<p:poll>` component for the job. See also the PrimeFaces `<p:poll>` showcase page whose code is extracted below:

View:

<h:form id="form">  
    <h:outputText id="txt_count" value="#{counterBean.count}" />  

    <p:poll interval="3"   
            listener="#{counterBean.increment}" update="txt_count" />  
</h:form>

Bean:

public class CounterBean implements Serializable{  

    private int count;  

    public int getCount() {  
        return count;  
    }  

    public void setCount(int count) {  
        this.count = count;  
    }  

    public void increment() {  
        count++;  
    }  
}  

In your particular case you thus need something like:

<p:poll interval="10" listener="#{bean.mymethod}" />  

Problem

Is it possible to fire an ajax event with JSF after page is loaded, for example on every 10 seconds? I mean: ``` <f:ajax listener="#{bean.mymethod()}" /> ``` I would like to invoke this method every 10 seconds passed after page is loaded. I don't want to reload the entire page by JavaScript refresh.

Original source