title: How to correctly start/stop a PrimeFaces Ajax Poll from javascript
body: <p>I want a little webapp for testing a connection to a server and returning a status message. After clicking a start button, I want to poll every 4 seconds and return a status text. But it will only return, if I click on the button, not automatically.
  Here is my code:</p>
  
  <p>index.xhtml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:p="http://primefaces.org/ui"&gt;
  
  &lt;h:head&gt;
  &lt;/h:head&gt;
      &lt;h:body&gt;
      &lt;h1&gt;My App&lt;/h1&gt;
          &lt;h:form onkeypress="poll.stop()"&gt;
              Server URL: &lt;h:inputText id="name" value="#{helloBean.name}"&gt;&lt;/h:inputText&gt;
              &lt;p:commandButton value="Start Watching!" onclick="poll.start()" /&gt;
  
              &lt;pre&gt;&lt;h:outputText id="output" value="#{helloBean.playground}" /&gt;&lt;/pre&gt;
              &lt;p:poll interval="4" listener="#{helloBean.getPlayground}" update="output" widgetVar="poll" autoStart="false" /&gt;
          &lt;/h:form&gt;
  &lt;/h:body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>And my bean:</p>
  
  <pre><code>@ManagedBean
  @SessionScoped
  public class HelloBean implements Serializable {
          public String getPlayground(){
          if(name.length() &lt; 3){
              return "";
          }
          DateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss");
          Date date = new Date();
          String date1 = dateFormat.format(date);
          int code = 0;
          String urldns = null;
          try{
          URL url = new URL(name);
          HttpURLConnection connection = (HttpURLConnection)url.openConnection();
          connection.setRequestMethod("GET");
          connection.connect();
          code = connection.getResponseCode();
          urldns = connection.getURL().toString();
          connection.disconnect();
          }
          catch(Exception ex){
              name2.add(date1+" Uhr : "+name+" -&gt; nicht erreichbar!");
          }
  
          if(code==200){
              if(urldns.contains("dnserror")){
                  name2.add(date1+" Uhr : "+name+" -&gt; nicht erreichbar!");
              }
              else{
                  name2.add(date1+" Uhr : "+name+" -&gt;  erreichbar ");
              }
          }
          startString = name+" wird überwacht. Taste drücken um abzubrechen...\n";
          String result = String.join("\n", name2);
          return startString+result;
      }
  }
  </code></pre>
  
  <p>Can anybody help me? cause I'm new to PF and Ajax. Thanks!</p>