Poll sends Ajax requests periodically. Here it calls a listener every 2 seconds (interval) that increments a number and updates its output. onactivated is a client side callback that is executed when the poll is activated, here it writes to the browser console.
<div class="card">
<h:form>
<h1 class="text-center">
<h:outputText id="txt_count" value="#{pollView.number}"/>
</h1>
<p:poll interval="2" listener="#{pollView.increment}" update="txt_count" onactivated="console.log('Poll started')"/>
</h:form>
</div>
package org.primefaces.showcase.view.ajax;
import java.io.Serializable;
import jakarta.faces.view.ViewScoped;
import jakarta.inject.Named;
@Named
@ViewScoped
public class PollView implements Serializable {
private int number;
public void increment() {
number++;
}
public int getNumber() {
return number;
}
}