Search code examples
javascriptjsfprimefacescommandlink

How to delay a commandLink in jsf with javascript or primefaces?


I have a commandLink which calls a backend page bean method. but I want to make a delay before it calls that method. How to do this using javascipt or primefaces?


Solution

  • PrimeFaces offers p:remoteCommand. It can be called via javascript and execute, do action and ajax process and updates. Use the p:commandLink to call a javascript function with a timeout. In short

    <p:commandLink onclick="delayIt()" />
    
    <p:remoteCommand name="remoteCommandName" ... action ... update ... process/>
    
    
    <script type="text/javascript">
        var delayIt = function(){
           setTimeout( remoteCommandName,5000) //don't write () as it would execute it immediately
        }
    </script>