In primary, there is ajaxStatus component that you can use for this purpose. Read the documentation to see more details about this, but for your use case it might be something like this:
<p:ajaxStatus oncomplete="ajaxStatusHandler(xhr, status, args)"/>
and you can use your JavaScript function as is:
function ajaxStatusHandler(xhr, status, args) {
NOTE. This method can only be used for global AJAX requests (which is used by default in PrimeFaces), as well as I know, a cross-domain script or JSONP (padded JSON) cannot be global.
source share